你真的会用UITableView嘛
2016-04-07 16:08
465 查看
UITableView是工程开发中最经常使用到的UI控件,但是你真的了解它嘛,这里记录几点有用的但你可能并不知道的。
当我们的数据未能显示满一屏幕的时候,UITableView会显示多余的横线,这个时候你如果希望去掉这些横线,你可以加上这句话。
self.tableView.tableFooterView = [[UIView alloc]init];
UITableView的分割线默认是开头空15像素点的(好像是15来着~~),产品经理有时候希望能够定格显示,那么你可能会这么做。
self.tableView.separatorInset = UIEdgeInsetsZero;
但是你很快就会发现这么做并没有效果,这是因为<code>separatorInset</code>这个属性在iOS7以后就已经失效了,但是我们还是能够达到同样的效果,你可以在你的tablevView的代理协议实现界面加上下面这段代码:
/** * 分割线顶头 */ -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)]; } } -(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]; } }
再次运行,好了我们的UITableView终于顶头显示分割线了。
– 很多情况下我们的UITableViewCell的高度是动态不确定的,比如说很多聊天的界面都需要我们去动态的计算cell的高度,你可能会在<code>heightForRowAtIndexPath</code>代理协议方法中返回你计算好的cell高度,然后在苹果推出约束以后,我们其实有更加方便的方法去实现相同的效果。你可以尝试在你的代码中加入以下两行代码:
self.tableView.estimatedRowHeight = 68.0; self.tableView.rowHeight = UITableViewAutomaticDimension;
再次运行你的程序,其实你发现了好像你的cell并没有动态的返回高度,这是因为上面说了,这两行代码必须配合约束来使用。
我们拖出一个SB,然后在cell上放上一个label,讲label的<code>numberOfLines</code>属性设置为0,然后设置好label的上下左右约束,然后再对label的内容进行赋值,再次运行你的程序,这个时候你的cell就会动态的显示高度了,label的高度取决于你的内容的多少,同时按照你的约束进行显示。
-你可能写过这样下面这样的代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:true]; [tableView beginUpdates]; ROW--;//此操作表示减少数据源的个数。 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; [tableView endUpdates]; }
用一个动画来删除某一个cell,其中有两行代码特别有意思:
[tableView beginUpdates]; [tableView endUpdates];
这俩吧其实和<code>[tableView reloadData]</code>作用类似,但是这俩货却能非常轻松的创造出不错的效果,比如说和我们上一点说的用约束来控制label的行高相结合的是的时候,我们先来看一下效果:
其实我的代码很少,核心代码只有以下几行:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:true]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UILabel *label = [cell.contentView viewWithTag:1000]; [tableView beginUpdates]; if (label.numberOfLines == 0) { label.numberOfLines = 1; }else{ label.numberOfLines = 0; } [tableView endUpdates]; }
我用SB创建了一个UITableView,然后在cell上放置了一个label,初始化label 的<code>numberOfLines</code>然后在界面上设置tableView
self.tableView.estimatedRowHeight = 68.0; self.tableView.rowHeight = UITableViewAutomaticDimension;
然后在他的点击动作中改变label的<code>numberOfLines</code>,同时结合使用:
[tableView beginUpdates]; [tableView endUpdates];
像上面po出来的代码那样,这个时候你如果使用[tableView reloadData]也能够达到改变cell高度的效果,但是界面上就不会有使用[tableView beginUpdates]那么流畅,以此类推,其实在很多地方都可以用<code>[tableView beginUpdates]</code>来代替<code>[tableView reloadData]</code>来达到更好的效果.
– 你可能会经常忽略UITableView的一些属性和回调,必须下面这个方法:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat offSet = tableView.contentOffset.y; if (offSet<=0) { return; } CGRect oldRect = cell.frame; CGRect newRect = cell.frame; newRect.origin.x += 50; cell.frame = newRect; [UIView animateWithDuration:0.5 animations:^{ cell.frame = oldRect; }]; }
如果你这么写会简单的有一个展示的动画,这个回调就是在cell展示到屏幕的时候发起的动作。
还有这个属性:<code>tableView.visibleCells</code>,你的产品经理可能会要求你的cell在滚动的时候进行一些展示类的动画—-滚动的时候进行展开收起之类的,这样的话你可以这么做:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ for (UITableViewCell *cell in _tableView.visibleCells) { /** * 你可以在这里对当前的cell进行一些操作 * */ } }
这个属性会返回即将展示到屏幕上的cell,而放在这个滚动的回掉中你就可以对你的cell进行不停的调整了,具体能做出什么动画,就靠你的想象能力了。
– tableView可能会造成你的Controller过于庞大,或许你可以使用MVVM类似的构架来瘦身你的Controller。。。。。。
相关文章推荐
- 苹果官方xcode工程build设置指南
- UGUI 下拉滚动框
- 解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
- 优化UITableViewCell高度计算的那些事
- UITableViewCell高度自适应探索--AutoLayout结合Frame
- IOS开发-UI学习-NSMutableAttributedString(带属性的字符串)的使用
- iOS开发基础学习笔记——导航控制器2->导航栏(UINavigationBar)/工具栏(toolBar)/导航元素按钮(UINavigationItem)
- easyui的datagrid的分页问题
- UITableView优化技巧
- 一个实现 Twitter SnowFlake 算法 的 Go 分布式 UID 生成器
- 改变UITextView的边框颜色
- UIView的contentMode使用小结
- 自定义UICollectionViewLayout(一) ----LineLayout
- UITextField小结
- 6.7.3 qmgr_message_reslove函数:解析收件人地址,生成QMGR_TRANSPORT和QMGR_QUEUE结构体
- 执行session.createSQLQuery(querySql).list()多次后,再次执行没有动静
- UIBarbuttonItem
- STL学习笔记— —容器deque
- maven install与maven build的区别
- JAVA中String与StringBuffer,StringBuilder的区别