willDisplayCell 是 UITableView 的一個(gè)代理方法,自定義顯示的方法一共有如上幾個(gè)。說到willDisplayCell,很多人會(huì)把它和cellForRowAtIndexPath做對(duì)比。
通過打斷點(diǎn)發(fā)現(xiàn),tableview 中先走cellForRowAtIndexPath方法,然后再走willDisplayCell方法。網(wǎng)上很多人建議,對(duì)于數(shù)據(jù)綁定填充放到willDisplayCell中執(zhí)行。但是通過試驗(yàn),一般情況下,在cellForRowAtIndexPath中進(jìn)行數(shù)據(jù)綁定填充,并不會(huì)造成卡頓和過度消耗性能的情況。特別復(fù)雜的 cell 除外。
willDisplayCell平時(shí)用的最多的是自定義 UITableView 分割線。系統(tǒng)默認(rèn)情況下,UITableView 的分割線左邊是沒有置頂?shù)?。打印它?inset 可以得到值為 {0, 15, 0, 0}
NSLog(@"%@", NSStringFromUIEdgeInsets(tableView.separatorInset));
如果想要分割線左右兩邊都頂格,有兩個(gè)方法。
- (void)viewDidLayoutSubviews {
if ([_tableView respondsToSelector: @selector(setSeparatorInset:)]) {
[_tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([_tableView respondsToSelector: @selector(setLayoutMargins:)]) {
[_tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
方法一只能修改整體分割線,如果要針對(duì)不同的 cell 設(shè)置不同的分割線,方法二比較適用。以后面對(duì)各種 cell 都不需要在自定義時(shí)再去拖個(gè) view 畫個(gè)分割線上去了。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。