2012-01-11 23 views
0

我試圖在我的tableView的右角做一個UIBarButtonItem,以突出顯示和取消突出顯示單元格時按下。用更少的話來說,當用戶按下按鈕時,一系列單元格會將它們的背景顏色從白色變爲黃色。如何通過點擊UIBarButtonItem來獲取UITableViewCell更改背景顏色?

雖然我沒有做到這一點,因爲每當我按下該按鈕,應用程序崩潰。

下面是我使用的創建按鈕的代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIBarButtonItem *barButton; 
    barButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"high.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(colorCells:)] autorelease]; 
    self.navigationItem.rightBarButtonItem = barButton; 
} 

在這裏,使它突出的單元格區域:

- (void) colorCells:(id)sender 
{ 
    UITableViewCell *cell; 
    NSString *cellValue = cell.textLabel.text; 
    if ([cellValue isEqual: @"textTheCellShouldBeEqualTo"]){ 
     cell.backgroundColor = [UIColor colorWithRed:251/255.0f green:255/255.0f blue:192/255.0f alpha:1]; ; 
     cell.imageView.image = [UIImage imageNamed:@"hot.png"]; 

    } 
    else { 
     cell.imageView.image = nil; 
     cell.backgroundColor = [UIColor whiteColor]; 
    } 
} 

我在哪裏失敗?它應該工作正常。要麼?我錯過了什麼嗎?該視圖是一個UITableViewController。

編輯

我修改我的代碼如下所示:

- (void) colorCells:(id)sender{ 
    UITableViewCell *cell; 
    NSInteger nSections = [self.tableView numberOfSections]; 
    for (int j=0; j<nSections; j++) { 
     NSInteger nRows = [self.tableView numberOfRowsInSection:j]; 
     for (int i=0; i<nRows; i++) { 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j]; 
      cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
     } 
     NSString *cellValue = cell.textLabel.text; 
     if ([cellValue isEqual: @"textTheCellShouldBeEqualTo"]){ 
      cell.backgroundColor = [UIColor colorWithRed:251/255.0f green:255/255.0f blue:192/255.0f alpha:1]; ; 
      cell.imageView.image = [UIImage imageNamed:@"hot.png"]; 

     } 
     else { 
      cell.imageView.image = nil; 
      cell.backgroundColor = [UIColor whiteColor]; 
     } 
     [self.tableView reloadData]; 

    } 
} 

現在,它不會崩潰,但它沒有顏色的背景了。有任何想法嗎?

+0

嘗試添加[self.tableView beginUpdates];和[self.tableView endUpdates];在末尾 – Novarg 2012-01-11 15:51:03

+0

仍然無法爲選定的單元格着色。 :S – Phillip 2012-01-11 15:53:13

回答

1

將背景顏色置於單元格中的文字看起來非常脆弱。什麼決定了一個單元格應該被突出顯示?它會改變嗎?當然,背景的顏色對應於數據源中對象的特定屬性。更可靠的方法是在類上使用NSMutableIndexSet屬性來跟蹤一組行索引,需要在點擊條形按鈕時突出顯示。

考慮這個例子。我假設rowsToHighlight是在您的類中聲明的NSMutableIndexSet的一個實例,並且它已填充需要突出顯示的行的索引。我們將執行-tableView:willDisplayCell:forRowAtIndexPath:來調整單元格的背景顏色,具體取決於提供的索引路徑是否爲rowsToHighlight的成員。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self.rowsToHighlight containsIndex:indexPath.row]) 
    { 
     cell.backgroundColor = [UIColor yellowColor]; 
    } 
} 

然後在由您的bar按鈕觸發的方法中,只需執行一個空的更新塊即可使用動畫重新加載表。

- (void)colorCells:(id)sender 
{ 
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 
} 
+0

嗯,謝謝你的回答。儘管我有幾個問題。 NSMutableIndexSet如何知道我想要哪些單元格,因爲我想僅突出顯示特定節中包含特定名稱的單元格(當我添加行並對它們進行排序時它可能是可變的)?我想做一個按鈕來處理背景顏色:當我點擊它時,單元格背景應該是黃色的,當我再次點擊它時,它應該再次變成白色。 – Phillip 2012-01-11 18:35:10

+0

這就是我的觀點。你用什麼樣的對象來填充你的表格視圖?如果它是某個暴露了指示是否應該突出顯示單元格的「BOOL」的NSObject子類,這將非常簡單。對象本身可以封裝確定是否應該突出顯示的所有邏輯。比較表格視圖數據源方法中的字符串充其量是笨重的。 – 2012-01-11 18:38:52

+0

我使用'NSArray'&'NSDictionary'來填充我的tV。對不起,如果我不明白這一點,但對於這個開發世界來說,我很新,所以剛剛寫的內容可能會讓我有點困惑! – Phillip 2012-01-11 18:45:06

0

除非您告訴包含它們以重新加載它們的表視圖,否則您的單元格將不會重新繪製。您可以使用以下幾種方法之一來完成此操作。你可以考慮[someTableView reloadData]one of these methods

另外,您的單元格對象不是您的表格視圖中的一個。你可能會考慮這個:

- (void) colorCells:(id)sender 
{ 
    UITableViewCell *cell; 

    //grab a cell 
    cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:someRow inSection:someSection]]; 
    NSString *cellValue = cell.textLabel.text; 
    if ([cellValue isEqual: @"textTheCellShouldBeEqualTo"]){ 
     cell.backgroundColor = [UIColor colorWithRed:251/255.0f green:255/255.0f blue:192/255.0f alpha:1]; ; 
     cell.imageView.image = [UIImage imageNamed:@"hot.png"]; 

    } 
    else { 
     cell.imageView.image = nil; 
     cell.backgroundColor = [UIColor whiteColor]; 
    } 

    //perhaps you need to reload the table as well 
    [self.tableView reloadData]; 
} 
+0

如果我有3個單獨的數組,每個數組都包含可變數目的單元格,'cellForRowAtIndexPath'如何工作? (即'FirstArray' = 13個對象,'SecondArray' = 45個對象,'ThirdArray' = 20個對象)我應該採用哪條路徑? – Phillip 2012-01-11 15:12:12

+0

你是在表中一次顯示一個數組,還是表的每個數組部分?索引路徑只是告訴表的委託,哪一行以及哪一部分給你一個表。 – Moshe 2012-01-11 15:19:58

+0

不是,每個數組都是表的一部分。我有3個部分,第一部分屬於'FirstArray',下面,第二部分屬於'SecondArray',第三部分屬於'ThirdArray'。那麼'indexPath'應該是什麼? – Phillip 2012-01-11 15:22:57

相關問題