自定義UITableViewCell的背景顏色


自定義UITableViewCell的背景顏色,實際上是對cell的contentView的背景顏色進行設置,所以可以有以下方法:

方法一:
cell.contentView.backgroundColor = [UIColor redColor];
方法二:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

 

以上是自定義cell. contentView的背景顏色或view的方法實現cell的自定義背景色,下面有UITableView的UITableViewDelegate方法,也可以實現此效果。代碼如下:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row%2==0) {

        cell.backgroundColor = [UIColorwhiteColor];

    }else {

        cell.backgroundColor = [UIColorlightGrayColor];

    }

 

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM