UITableView 是iOS開發(fā)中經(jīng)常使用的贞间,它的功能也非常強大坝锰,使用也非常方便。
在使用它的多組時今魔,默認的組間距非常的大勺像,看起來非常難受。那么我們這里就介紹一下如何設(shè)置自定義的組間距错森。
首先吟宦,我們要知道組間距是由什么構(gòu)成的,其實它的原理就是涩维,顯示效果的section是由section的頭視圖和腳視圖的組合殃姓,那么我們就可以分別對頭視圖和腳視圖進行代理設(shè)置即可。
//頭視圖高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 10;
}
//腳視圖高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 5;
}
這樣就可以實現(xiàn)自定義的組間距了瓦阐。
還可以自定義頭視圖和腳視圖的填充內(nèi)容
//頭視圖高度
- (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;
}