如何更改组UITableView中第一行和最后一行的颜色?

问题描述:

如何更改组UITableView中第一行和最后一行的颜色?如何更改组UITableView中第一行和最后一行的颜色?

我想与你分享我的解决方案,但我不知道如何,所以我创建了这个问题。对不起。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    [self configureCell:cell atIndexPath:indexPath]; 


    for (int x = 0; x < [[self.fetchedResultsController sections] count]; x++) { 

     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:x]; 

     int d = [sectionInfo numberOfObjects]; 

     if(indexPath.section == x){ 

      if (indexPath.row == 0 || indexPath.row == d-1) { 

       cell.backgroundColor = [UIColor blackColor]; 

      }else{ 

       cell.backgroundColor = [UIColor whiteColor]; 

      } 

     } 
    } 

    return cell; 
} 

在你UITableViewDataSource方法tableView:cellForRowAtIndexPath:做这样的事情,你的细胞已经成立后:

if (indexPath.row == 0 ||  
    indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) { 
    cell.backgroundColor = [UIColor redColor]; // or anything you want. 
} 
+0

看看我的答案。您可以分别更改第一个或最后一个单元格或中间单元格的颜色。 – NamshanNet

+0

Yaser,如果你发布了一个问题并想自己回答,请选中**回答自己的问题**选项,这样问题才会显示出来,直到你发布了你的答案。 http://blog.*.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – DrummerB