Setting background color of a table view cell on iPhone

后端 未结 12 746
孤街浪徒
孤街浪徒 2020-12-02 11:23

I want to achieve the effect where one cell of the table view will have blue background, the next one will have white, the next one will have blue again, and then white and

12条回答
  •  时光说笑
    2020-12-02 11:47

    Add this method to your table view delegate:

    #pragma mark UITableViewDelegate
    - (void)tableView: (UITableView*)tableView 
      willDisplayCell: (UITableViewCell*)cell 
    forRowAtIndexPath: (NSIndexPath*)indexPath
    {
        cell.backgroundColor = indexPath.row % 2 
            ? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] 
            : [UIColor whiteColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    }
    

提交回复
热议问题