通过修改父类,改变子类的cell的背景颜色及图片

本文介绍了一种在UITableView中实现便签效果的方法。通过在UITableView的父类中重写willDisplayCell方法,根据不同indexPath设置不同的背景图片来实现列表项的便签样式。此外,还介绍了如何移除列表项之间的分割线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">因项目需求,需要将项目中所有tableView的列表做成便签效果,若在每个页面分别修改下类太过麻烦</span>

<span style="font-size:18px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</span>

好在这些页面都集成于一个UITableView父类,于是在父类中加入如下代码:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor clearColor];

    UIImage *cellImage;
    if (indexPath.row == 0) {
        
        cellImage = [UIImage imageNamed:@"列表背景_上.png"];
    }else if (indexPath.row == [self.tableView numberOfRowsInSection:0]-1){
        cellImage = [UIImage imageNamed:@"列表背景_下.png"];
    }else {
        cellImage = [UIImage imageNamed:@"列表背景_中.png"];
    }
    
    UIImageView *cellImageView = [[UIImageView  alloc]initWithFrame:cell.frame];
    [cellImageView setImage:cellImage];
    cell.backgroundView = cellImageView;

 }

并在父类中修改

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值