近期的项目,小编经常会遇到关于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;
}
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;
}