IOS UITableView 常见问题总结(-)

本文针对TableView使用过程中出现的典型问题提供了详细的解决方案,包括调整样式去除多余空间、自定义头部样式及实现单元格编辑功能。

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

近期的项目,小编经常会遇到关于tableview 的问题,在这里进行一下总结,希望也能帮助其他同学...


1. 在使用tableview时,如果发现tableview的第一个cell上多出一块,想要取消可以尝试下面的方式:

1). 将tableview的样式设置成plain,

2). 如果section有多组,将第一组的header高度设置为0;这样几本就可以解决了

 

2. 自定义headerInSection的显示样式,代码如下,还有很重要的一点,<TABleView中每组的header和footer时链接在一起的,如果想要内容在各组间居中显示,一定要将其中一个headerview活着footerview隐藏,应该就可以满足需求>

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


    UIView *customView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, screenWidth,30)];

    

    UILabel *headerLab = [[UILabelalloc]init];

    

    [customView addSubview:headerLab];

    

    [headerLab mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.left.mas_equalTo(customView.left).offset(12);

        make.centerY.mas_equalTo(customView);

        

    }];

    if (section ==1) {

        headerLab.text =@"高端品牌";

        customView.backgroundColor = UIColorFromRGBA(0xf0f0f0, 1.0);

        

    }else if(section ==2){

    

        headerLab.textColor = [UIColorwhiteColor];

        headerLab.text = @"最新车源";

        customView.backgroundColor = UIColorFromRGBA(0x5ca6e9, 1.0);

        


    }else{


        headerLab.text =@"";

    

    }


    return customView;

}


3. 实现cell自定义编辑功能
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    UITableViewRowAction *edit = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"编辑"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath * _Nonnull indexPath) {

        

        

        

    }];

    

    edit.backgroundColor = [UIColorcolorWithRed:11/256.0green:93/256.0blue:213/256.0alpha:1];

    

    UITableViewRowAction *delete = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"删除"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath * _Nonnull indexPath) {

        

// 1. 实现点击功能,多为对于数据源的增删改

        // 2.刷新表格

        [self.tableView reloadData];

        

    }];

    

    delete.backgroundColor = [UIColorcolorWithRed:11/256.0green:93/256.0blue:213/256.0alpha:1];

    

    NSArray *arr = @[edit,delete];

    

    return arr;

    

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值