今天在設(shè)置tableviewCell的分割線(xiàn)的時(shí)候,嘗試了幾種方法
這里涉及到兩個(gè)概念:
tableview的屬性:
tableFooterView
Tableview的代理方法中傅事,設(shè)置的section的
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
和- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
這里分析下兩個(gè)的區(qū)別:
tableviewFooterView,是整個(gè)tableview的footer
而代理方法中設(shè)置的壕吹,是每個(gè)section footer的高度和頁(yè)面
然后來(lái)說(shuō)tableview中設(shè)置分割線(xiàn)的問(wèn)題:
- 上面說(shuō)的兩條屬性都不設(shè)置的時(shí)候:是這樣的效果
然后我們只設(shè)置代理:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
效果如下:
然后我們可以修改一些參數(shù):
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [WDTool createLineWithFrame:CGRectMake(15, 0, SCREENWIDTH - 30, 0.5) color:UIColorFromRGB(0xdddddd)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.5f;
}
比如生成一條0.5px的線(xiàn)央拖,效果又變成了這樣:
但是有個(gè)問(wèn)題:我們?cè)O(shè)置生成的線(xiàn)是距離左右邊各15px的距離箫踩,但是效果圖可以看到是左右貫穿的腹备。
所以哎迄,如果需求是要設(shè)置全部cell的分割線(xiàn)是左右貫穿的話(huà)回右,可以使用這種方法
首先要配置cell的separatorInset屬性為.Zero
cell.separatorInset = UIEdgeInsetsZero;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [WDTool createLineWithFrame:CGRectMake(0, 0, SCREENWIDTH, 0.5) color:UIColorFromRGB(0xdddddd)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.5f;
}
效果圖:
然后我們只設(shè)置 self.tableview.tableFooterView = [UIView new];
可以看到這個(gè)就是我們想要的結(jié)果:
第一行上邊沒(méi)有分割線(xiàn)隆圆,最后一行下邊有分割線(xiàn)。
總結(jié)一下:
知識(shí)點(diǎn):
Cell的separatorInset屬性, 可以設(shè)置分割線(xiàn)的間距(.Zero屬性為貫穿屏幕)
tableview的separatorStyle屬性:可以設(shè)置有沒(méi)有分割線(xiàn)
tableview的sectionFooter代理方法:如果返回了空view翔烁,則沒(méi)效果渺氧,如果返回了非空view,則可隱藏沒(méi)有數(shù)據(jù)的cell的分割線(xiàn),然后在section最后一個(gè)cell的底部生成一個(gè)貫穿屏幕的高度自定的view
tableview的tableFooterView屬性:如果設(shè)置等于空view租漂,則可以隱藏沒(méi)有數(shù)據(jù)的cell的分割線(xiàn)阶女,然后在最后一個(gè)section的最后一個(gè)cell上,添加一個(gè)跟cell是separatorInset屬性匹配的分割線(xiàn)哩治。
案例:
上下都要分割線(xiàn)的情況:
自定義cell秃踩,然后在cell上自己放兩個(gè)線(xiàn),然后設(shè)置 self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;最上面沒(méi)有分割線(xiàn)业筏,最下邊也沒(méi)分割線(xiàn):
設(shè)置代理方法憔杨,返回一個(gè)空的view([UIView new])高度返回 CGFloat_MIN
- 最上面沒(méi)有分割線(xiàn),最下邊有貫穿的分割線(xiàn)(跟別的cell的分割線(xiàn)風(fēng)格不匹配)
設(shè)置代理方法蒜胖,返回一個(gè)非空的view并設(shè)置想要的高度消别,即可
- 最上面沒(méi)有分割線(xiàn),最下邊有跟別的cell匹配的分割線(xiàn)
只設(shè)置 self.tableview.tableFooterView = [UIView new];即可