iOS小技能: tableView section间距失效的解决方案

2,489 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第33天,点击查看活动详情

前言

tableView 一些常用的细节技巧:

I section相关

1.1 section的间距失效的解决方案

iOS tableView设置style:UITableViewStyleGrouped 时,非第一个section的间距失效的解决方案: 必须全部实现FooterInSectionFooterInSection对应的四个代理方法才有效(四个必须同时实现)

具体的代码如下


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    switch (section) {
        case ERPRelease_commoditiesViewSection4platProductCategories:
        case ERPRelease_commoditiesViewSection4productCategoriesbasicInfo4Singlespecification:
            
            case ERPRelease_commoditiesViewSection4brandInfo:

            
            {
                return kAdjustRatio(10);

            }
            break;
            
            
            
        default:
            break;
    }
    
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;


}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *tmp = [UIView new];

    tmp.backgroundColor = self.tableView.backgroundColor;

    switch (section) {
        case ERPRelease_commoditiesViewSection4platProductCategories:
        case ERPRelease_commoditiesViewSection4productCategoriesbasicInfo4Singlespecification:
            case ERPRelease_commoditiesViewSection4brandInfo:


            {
                return tmp;


            }
            break;



        default:
            break;
    }

    return nil;
}

  • 效果图:

在这里插入图片描述

  • 表格的初始化代码

- (UITableView *)tableView{
    if (nil == _tableView) {
//        UITableView *tmpView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];//初始化方法
        UITableView *tmpView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];//初始化方法
        _tableView = tmpView;
        _tableView.bounces = NO;
        tmpView.delegate = self;
        tmpView.rowHeight = UITableViewAutomaticDimension;
        tmpView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tmpView.dataSource = self;
        [self addSubview:_tableView];
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, kAdjustRatio(0),0);

        
        __weak __typeof__(self) weakSelf = self;
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf);
            make.left.equalTo(weakSelf).offset(kAdjustRatio(0));
            make.bottom.equalTo(weakSelf);
            
            make.right.equalTo(weakSelf).offset(-kAdjustRatio(0));
            
            
        }];
        
        [_tableView setBackgroundColor:k_view_backColor];
        
        
    }
    return _tableView;
}


1.2 修改 SectionHeader 字体及背景色

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{

    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;

    header.textLabel.textColor = rgb(51,51,51);
    header.textLabel.font = kPingFangFont(15);

    [header layoutIfNeeded];

    header.clipsToBounds = YES;

        header.contentView.backgroundColor
        = self.tableView.backgroundColor;

}

1.3 自定义FooterView

II 为UITableViewCell设置预估高度

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return kAdjustRatio(44);
}



III 自定义cell样式

3.1 显示Checkmark样式

- (void)setModels:( ERPProductCategoryTreeDto*)models{
    
    _models =models;
    
    
    
    
    if(models.selectedStyle == ProductCategorytTreeSelectedStyle4Checkmark){

        if (models.isSelected ) {
            
            self.tintColor = HWColor(243, 39, 52);
            self.accessoryType = UITableViewCellAccessoryCheckmark;
        }else{
            self.accessoryType = UITableViewCellAccessoryNone;

        }
     }
}

————————————————
版权声明:本文为CSDN博主「iOS逆向」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z929118967/article/details/106691892

3.2 案例: 商品类目选择视图

[video(video-fWYLiH0s-1624340333311)(type-csdn)(url-live.csdn.net/v/embed/167… 商品/经营类目选择视图)]

下载地址:https://download.csdn.net/download/u011018979/19775162

文章地址:kunnan.blog.csdn.net/article/det… 视频地址:live.csdn.net/v/167208 商品经营类目选择视图的应用场景: 1、发布商品时选择商品类目 2、商户进件选择经营类目 3、购物类app下单界面的商品类目筛选

在发布商品的时候,选择类目界面的要求视图分为上下部分。

1、 上部分:展示已经选择的类目信息,并清晰的从上倒下罗列对应层级类目信息(悬浮),点击类目的时候,下部分的展示的类目信息切换为同级类目信息供选择。 2、 下部分:展示可供选择的类目信息(支持滚动选中类目)

支持清空数据功能

see also

小程序:iOS逆向