0
点赞
收藏
分享

微信扫一扫

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


前言

tableView 一些常用的细节技巧:

  • iOS tableView设置style:UITableViewStyleGrouped 时,非第一个section的间距失效】的解决方案
  • 修改 tableViewSectionHeader 字体及背景色
  • 为UITableViewCell设置预估高度
  • iOS 自定义UITableViewHeaderFooterView (替代titleForHeaderInSection)

I section相关

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

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

具体的代码如下

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;
}

  • 效果图:

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

  • 表格的初始化代码

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

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

  • iOS 自定义UITableViewHeaderFooterView (替代titleForHeaderInSection)

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 案例: 商品类目选择视图


商品经营类目选择视图的应用场景: 1、发布商品时选择商品类目 2、商户进件选择经营类目 3、购物类app下单界面的商品类目筛选

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

支持清空数据功能



举报

相关推荐

0 条评论