2012-01-11 22 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

相关问题