UITableView 是iOS开发中经常使用的,它的功能也非常强大,使用也非常方便。
在使用它的多组时,默认的组间距非常的大,看起来非常难受。那么我们这里就介绍一下如何设置自定义的组间距。
首先,我们要知道组间距是由什么构成的,其实它的原理就是,显示效果的section是由section的头视图和脚视图的组合,那么我们就可以分别对头视图和脚视图进行代理设置即可。
//头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 10;
}
//脚视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 5;
}
这样就可以实现自定义的组间距了。
还可以自定义头视图和脚视图的填充内容
//头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
return headerView;
}
//脚视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc] init];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}
网友评论