我试图在不同颜色的表视图中放置一些单元格。问题是它们只有在退出应用程序后才会变色。每次我创建一个新单元格时,它都会被默认颜色着色(但退出应用程序或模拟器并重新打开后,它们都会根据我的需要进行着色。)
添加单元格后,我应该怎么做才能刷新颜色?谢谢!
这是我的代码:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==0) cell.contentView.backgroundColor = [UIColor colorWithRed:36.0/255.0 green:230.0/255.0 blue:146.0/255.0 alpha:1.0f];
if(indexPath.row==1) cell.contentView.backgroundColor = [UIColor colorWithRed:72.0/255.0 green:214.0/255.0 blue:202.0/255.0 alpha:1.0f];
if(indexPath.row==2) cell.contentView.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:132.0/255.0 blue:255.0/255.0 alpha:1.0f];
if(indexPath.row==3) cell.contentView.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:84.0/255.0 blue:255.0/255.0 alpha:1.0f];
if(indexPath.row==4) cell.contentView.backgroundColor = [UIColor colorWithRed:243.0/255.0 green:207.0/255.0 blue:7.0/255.0 alpha:1.0f];
if(indexPath.row==5) cell.contentView.backgroundColor = [UIColor colorWithRed:243.0/255.0 green:157.0/255.0 blue:7.0/255.0 alpha:1.0f];
}
答案 0 :(得分:0)
这似乎是你应该在方法cellForRowAtIndexPath
中做的事情,它返回给定索引路径的单元格。作为单元格设置的一部分,您可以设置它的背景颜色。
答案 1 :(得分:0)
工作代码。
1)设置小区标识符
2)在您的.m文件中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if(indexPath.row==0) cell.contentView.backgroundColor = [UIColor colorWithRed:36.0/255.0 green:230.0/255.0 blue:146.0/255.0 alpha:1.0f];
if(indexPath.row==1) cell.contentView.backgroundColor = [UIColor colorWithRed:72.0/255.0 green:214.0/255.0 blue:202.0/255.0 alpha:1.0f];
if(indexPath.row==2) cell.contentView.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:132.0/255.0 blue:255.0/255.0 alpha:1.0f];
if(indexPath.row==3) cell.contentView.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:84.0/255.0 blue:255.0/255.0 alpha:1.0f];
if(indexPath.row==4) cell.contentView.backgroundColor = [UIColor colorWithRed:243.0/255.0 green:207.0/255.0 blue:7.0/255.0 alpha:1.0f];
if(indexPath.row==5) cell.contentView.backgroundColor = [UIColor colorWithRed:243.0/255.0 green:157.0/255.0 blue:7.0/255.0 alpha:1.0f];
return cell;
}
答案 2 :(得分:-1)
这就是我解决问题的方法:
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
这是我的最终代码:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
} }