一個(gè)app基本少不了UITableView电谣,甚至有的app一半以上的視圖都使用了UITableView秽梅,所以UITableView在iOS中占據(jù)著重要的位置。以下用來(lái)記錄一個(gè)UITableView使用的過(guò)程中的一些小知識(shí)剿牺。
-
簡(jiǎn)單炫酷又好像沒(méi)啥用的動(dòng)畫(huà)
要實(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];
-
刪除
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
查看打印信息:
- insert
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分割線
uitableviewcell的默認(rèn)分割線左邊會(huì)有15像素的空白钱反,如果想要一條全屏的分割線,要么分割線風(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];
}