iOS UITableView小記錄

一個(gè)app基本少不了UITableView,甚至有的app一半以上的視圖都使用了UITableView,所以UITableView在iOS中占據(jù)著重要的位置。以下用來(lái)記錄一個(gè)UITableView使用的過(guò)程中的一些小知識(shí)。

  • 簡(jiǎn)單炫酷又好像沒(méi)啥用的動(dòng)畫(huà)

huadddd.gif

要實(shí)現(xiàn)這個(gè)效果,只需要在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath里面:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    //還原
    [UIView animateWithDuration:1 animations:^{
        cell.layer.transform = CATransform3DIdentity;
    }];
}
  • cell自帶樣式的顏色

  • 通過(guò)tintcolor設(shè)置
  • cell.tintColor = [UIColor blackColor];
1.jpg
  • 刪除

UITableView 編輯狀態(tài)下 , 刪除cell 時(shí), 一般用
[_tableView deselectRowAtIndexPath:@[indexPath] animated:YES];
進(jìn)行刪除單行操作,但是在多個(gè)section里面進(jìn)行刪除單行時(shí)可能報(bào)以下錯(cuò)誤:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:1704

因?yàn)楫?dāng)其中一個(gè)section刪除僅剩下一個(gè)cell時(shí),再進(jìn)行刪除就需要?jiǎng)h除整個(gè)section,這時(shí)可以使用:
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

  • 插入或者重載

    很多需求可能會(huì)用到不規(guī)則cell列表,而調(diào)用insert和reload方法都會(huì)遍歷一遍全部cell高度獲取函數(shù)。

    • insert
      接收到新的數(shù)據(jù)時(shí),加入數(shù)據(jù)源之后,使用insert添加cell
      查看打印信息:

2016-06-29 11:22:49.799[1318:49268] insert
2016-06-29 11:22:49.799[1318:49268] 進(jìn)入numberOfSectionsInTableView:
2016-06-29 11:22:49.800[1318:49268] 進(jìn)入heightForRowAtIndexPath:
...
2016-06-29 11:22:49.808[1318:49268] 進(jìn)入heightForRowAtIndexPath:


  *  reload
接收到新的數(shù)據(jù)時(shí),加入數(shù)據(jù)源之后,直接調(diào)用reload
查看打印信息:

2016-06-29 11:27:39.899[1318:49268] reload
2016-06-29 11:27:39.900[1318:49268] 進(jìn)入numberOfSectionsInTableView:
2016-06-29 11:27:39.900[1318:49268] 進(jìn)入numberOfRowsInSection:
2016-06-29 11:27:39.900[1318:49268] 進(jìn)入heightForRowAtIndexPath:
...
2016-06-29 11:27:39.907[1318:49268] 進(jìn)入heightForRowAtIndexPath:
2016-06-29 11:27:39.923[1318:49268] 進(jìn)入heightForRowAtIndexPath:
2016-06-29 11:27:39.928[1318:49268] 進(jìn)入cellForRowAtIndexPath:
2016-06-29 11:27:39.929[1318:49268] 進(jìn)入heightForRowAtIndexPath:
2016-06-29 11:27:39.929[1318:49268] 進(jìn)入cellForRowAtIndexPath:
...
2016-06-29 11:27:40.008[1318:49268] 進(jìn)入heightForRowAtIndexPath:
2016-06-29 11:27:40.092[1318:49268] 進(jìn)入cellForRowAtIndexPath:


  >  insert和reload的不同在于如果insert的cell不是即將顯示的,就不會(huì)重新遍歷屏幕中的cell。
所以要提高tableview效率:
>  1 在返回高度的協(xié)議中``- (CGFloat)tableView:(UITableView  *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath``最好只進(jìn)行返回高度,而不是計(jì)算完高度后返回。

 > 2 如果``- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath``中處理的事件比較多,那最好還是用insert來(lái)添加cell。




* ###  cell分割線(xiàn)
 uitableviewcell的默認(rèn)分割線(xiàn)左邊會(huì)有15像素的空白,如果想要一條全屏的分割線(xiàn),要么分割線(xiàn)風(fēng)格設(shè)置``UITableViewCellSeparatorStyleNone``,然后自己加一條,要么設(shè)置tableview和cell的SeparatorInset:
  在viewDidLayoutSubviews中

  • (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    }

 在cell設(shè)置里面:

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}

最后編輯于
?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容