zoukankan      html  css  js  c++  java
  • 你真的会用UITableView嘛

    UITableView是工程开发中最经常使用到的UI控件,但是你真的了解它嘛,这里记录几点有用的但你可能并不知道的。

    • 当我们的数据未能显示满一屏幕的时候,UITableView会显示多余的横线,这个时候你如果希望去掉这些横线,你可以加上这句话。
     1 self.tableView.tableFooterView = [[UIView alloc]init]; 
    
    • UITableView的分割线默认是开头空15像素点的(好像是15来着~~),产品经理有时候希望能够定格显示,那么你可能会这么做。
     1 self.tableView.separatorInset = UIEdgeInsetsZero; 
    

    但是你很快就会发现这么做并没有效果,这是因为<code>separatorInset</code>这个属性在iOS7以后就已经失效了,但是我们还是能够达到同样的效果,你可以在你的tablevView的代理协议实现界面加上下面这段代码:

     1 /**
     2 * 分割线顶头
     3 */
     4 -(void)viewDidLayoutSubviews
     5 {
     6 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
     7 [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
     8 }
     9 
    10 if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    11 [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    12 }
    13 }
    14 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    15 {
    16 if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
    17 [cell setSeparatorInset:UIEdgeInsetsZero];
    18 }
    19 if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    20 [cell setLayoutMargins:UIEdgeInsetsZero];
    21 }
    22 }

    再次运行,好了我们的UITableView终于顶头显示分割线了。
    – 很多情况下我们的UITableViewCell的高度是动态不确定的,比如说很多聊天的界面都需要我们去动态的计算cell的高度,你可能会在<code>heightForRowAtIndexPath</code>代理协议方法中返回你计算好的cell高度,然后在苹果推出约束以后,我们其实有更加方便的方法去实现相同的效果。你可以尝试在你的代码中加入以下两行代码:

     1 self.tableView.estimatedRowHeight = 68.0;
    2 self.tableView.rowHeight = UITableViewAutomaticDimension;
     

    再次运行你的程序,其实你发现了好像你的cell并没有动态的返回高度,这是因为上面说了,这两行代码必须配合约束来使用。
    我们拖出一个SB,然后在cell上放上一个label,讲label的<code>numberOfLines</code>属性设置为0,然后设置好label的上下左右约束,然后再对label的内容进行赋值,再次运行你的程序,这个时候你的cell就会动态的显示高度了,label的高度取决于你的内容的多少,同时按照你的约束进行显示。
    -你可能写过这样下面这样的代码

    1 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    2 [tableView deselectRowAtIndexPath:indexPath animated:true];
    3 [tableView beginUpdates];
    4 ROW--;//此操作表示减少数据源的个数。
    5 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    6 [tableView endUpdates];
    7 }

    用一个动画来删除某一个cell,其中有两行代码特别有意思:

     1 [tableView beginUpdates]; 

    2 [tableView endUpdates];
     

    这俩吧其实和<code>[tableView reloadData]</code>作用类似,但是这俩货却能非常轻松的创造出不错的效果,比如说和我们上一点说的用约束来控制label的行高相结合的是的时候,我们先来看一下效果:

    10
    其实我的代码很少,核心代码只有以下几行:

     1 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     2 [tableView deselectRowAtIndexPath:indexPath animated:true];
     3 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     4 UILabel *label = [cell.contentView viewWithTag:1000];
     5 [tableView beginUpdates];
     6 if (label.numberOfLines == 0) {
     7 label.numberOfLines = 1;
     8 }else{
     9 label.numberOfLines = 0;
    10 }
    11 [tableView endUpdates];
    12 }

    我用SB创建了一个UITableView,然后在cell上放置了一个label,初始化label 的<code>numberOfLines</code>然后在界面上设置tableView

     1 self.tableView.estimatedRowHeight = 68.0;
    2 self.tableView.rowHeight = UITableViewAutomaticDimension;
     

    然后在他的点击动作中改变label的<code>numberOfLines</code>,同时结合使用:

     1 [tableView beginUpdates]; 
    2 [tableView endUpdates];
     

    像上面po出来的代码那样,这个时候你如果使用[tableView reloadData]也能够达到改变cell高度的效果,但是界面上就不会有使用[tableView beginUpdates]那么流畅,以此类推,其实在很多地方都可以用<code>[tableView beginUpdates]</code>来代替<code>[tableView reloadData]</code>来达到更好的效果.
    – 你可能会经常忽略UITableView的一些属性和回调,必须下面这个方法:

     1 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
     2 CGFloat offSet = tableView.contentOffset.y;
     3 if (offSet<=0) {
     4 return;
     5 }
     6 CGRect oldRect = cell.frame;
     7 CGRect newRect = cell.frame;
     8 newRect.origin.x += 50;
     9 cell.frame = newRect;
    10 [UIView animateWithDuration:0.5 animations:^{
    11 cell.frame = oldRect;
    12 }];
    13 }

    如果你这么写会简单的有一个展示的动画,这个回调就是在cell展示到屏幕的时候发起的动作。
    还有这个属性:<code>tableView.visibleCells</code>,你的产品经理可能会要求你的cell在滚动的时候进行一些展示类的动画—-滚动的时候进行展开收起之类的,这样的话你可以这么做:

    1 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    2 for (UITableViewCell *cell in _tableView.visibleCells) {
    3 /**
    4 * 你可以在这里对当前的cell进行一些操作
    5 *
    6 */
    7 }
    8 }

    这个属性会返回即将展示到屏幕上的cell,而放在这个滚动的回掉中你就可以对你的cell进行不停的调整了,具体能做出什么动画,就靠你的想象能力了。
    – tableView可能会造成你的Controller过于庞大,或许你可以使用MVVM类似的构架来瘦身你的Controller。。。。。。

  • 相关阅读:
    DDOS攻击事件记录
    ansible批量安装zabbix客户端并实现自动发现功能
    利用api更新域名解析ip+端口转发【2】
    利用api更新域名解析ip+端口转发【1】
    网站春节开市休市设置
    获取内网路由器管理页面出口ip
    关于nginx加载配置文件的巨坑
    活动封禁刷票ip
    二十五个Python高级开发技巧,终极干货!建议收藏学习!
    一则故事带你秒懂Python GIL原理!
  • 原文地址:https://www.cnblogs.com/fengmin/p/5364047.html
Copyright © 2011-2022 走看看