iOS -- DemoIos开发学习Objective C开发

iOS UITableView小记录

2016-06-30  本文已影响933人  funnyS

一个app基本少不了UITableView,甚至有的app一半以上的视图都使用了UITableView,所以UITableView在iOS中占据着重要的位置。以下用来记录一个UITableView使用的过程中的一些小知识。

huadddd.gif

要实现这个效果,只需要在- (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;
    }];
}
1.jpg

UITableView 编辑状态下 , 删除cell 时, 一般用
[_tableView deselectRowAtIndexPath:@[indexPath] animated:YES];
进行删除单行操作,但是在多个section里面进行删除单行时可能报以下错误:

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

因为当其中一个section删除仅剩下一个cell时,再进行删除就需要删除整个section,这时可以使用:
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

2016-06-29 11:22:49.799[1318:49268] insert
2016-06-29 11:22:49.799[1318:49268] 进入numberOfSectionsInTableView:
2016-06-29 11:22:49.800[1318:49268] 进入heightForRowAtIndexPath:
...
2016-06-29 11:22:49.808[1318:49268] 进入heightForRowAtIndexPath:


  *  reload
接收到新的数据时,加入数据源之后,直接调用reload
查看打印信息:

2016-06-29 11:27:39.899[1318:49268] reload
2016-06-29 11:27:39.900[1318:49268] 进入numberOfSectionsInTableView:
2016-06-29 11:27:39.900[1318:49268] 进入numberOfRowsInSection:
2016-06-29 11:27:39.900[1318:49268] 进入heightForRowAtIndexPath:
...
2016-06-29 11:27:39.907[1318:49268] 进入heightForRowAtIndexPath:
2016-06-29 11:27:39.923[1318:49268] 进入heightForRowAtIndexPath:
2016-06-29 11:27:39.928[1318:49268] 进入cellForRowAtIndexPath:
2016-06-29 11:27:39.929[1318:49268] 进入heightForRowAtIndexPath:
2016-06-29 11:27:39.929[1318:49268] 进入cellForRowAtIndexPath:
...
2016-06-29 11:27:40.008[1318:49268] 进入heightForRowAtIndexPath:
2016-06-29 11:27:40.092[1318:49268] 进入cellForRowAtIndexPath:


  >  insert和reload的不同在于如果insert的cell不是即将显示的,就不会重新遍历屏幕中的cell。
所以要提高tableview效率:
>  1 在返回高度的协议中``- (CGFloat)tableView:(UITableView  *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath``最好只进行返回高度,而不是计算完高度后返回。

 > 2 如果``- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath``中处理的事件比较多,那最好还是用insert来添加cell。




* ###  cell分割线
 uitableviewcell的默认分割线左边会有15像素的空白,如果想要一条全屏的分割线,要么分割线风格设置``UITableViewCellSeparatorStyleNone``,然后自己加一条,要么设置tableview和cell的SeparatorInset:
  在viewDidLayoutSubviews中

 在cell设置里面:

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

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

上一篇下一篇

猜你喜欢

热点阅读