zoukankan      html  css  js  c++  java
  • UITableView 的一些奇淫技巧1

    http://www.strongx.cn/  感谢这位老兄

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


    • 当我们的数据未能显示满一屏幕的时候,UITableView


    self.tableView.tableFooterView = [[UIView alloc]init];


    • UITableView1515


       self.tableView.separatorInset = UIEdgeInsetsZero;


    但是你很快就会发现这么做并没有效果,这是因为separatorInset这个属性在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

    很多情况下我们的UITableViewCellcell的高度,你可能会在heightForRowAtIndexPath代理协议方法中返回你计算好的cell高度,然后在苹果推出约束以后,我们其实有更加方便的方法去实现相同的效果。你可以尝试在你的代码中加入以下两行代码:


    self.tableView.estimatedRowHeight = 68.0;

    self.tableView.rowHeight = UITableViewAutomaticDimension;


    再次运行你的程序,其实你发现了好像你的cell并没有动态的返回高度,这是因为上面说了,这两行代码必须配合约束来使用。


    我们拖出一个SB,然后在cell上放上一个label,讲label的numberOfLines属性设置为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];


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


    一个tableView点击缩放的效果

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


    -(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 的numberOfLines然后在界面上设置tableView


    self.tableView.estimatedRowHeight = 68.0;

    self.tableView.rowHeight = UITableViewAutomaticDimension;


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


    [tableView beginUpdates];

     [tableView endUpdates];


    像上面po出来的代码那样,这个时候你如果使用[tableView reloadData]也能够达到改变cell高度的效果,但是界面上就不会有使用[tableView beginUpdates]那么流畅,以此类推,其实在很多地方都可以用来代替[tableView reloadData]来达到更好的效果.


    你可能会经常忽略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展示到屏幕的时候发起的动作。

    还有这个属性:tableView.visibleCells,你的产品经理可能会要求你cell—-

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{

      for (UITableViewCell *cell in _tableView.visibleCells) {

          /**

           *  你可以在这里对当前的cell进行一些操作

           *

           */

      }

    }


    这个属性会返回即将展示到屏幕上的cell,而放在这个滚动的回掉中你就可以对你的cell进行不停的调整了,具体能做出什么动画,就靠你的想象能力了。


    tableView可能会造成你的Controller过于庞大,或许你可以使用MVVM类似的构架来瘦身你的Controller。。。。。。

  • 相关阅读:
    vue生命周期
    SQL Server 2008教程和Microsoft® SQL Server® 2008 R2 SP2
    min.css----全世界最快的CSS框架
    4分钟网页设计(译文)
    CSS 选择器
    CSS 定位
    [书]WALL·E、龙与地下铁、中国美丽的故事、故事新编、四十自述、书虫、人工智能、大话数据结构
    Underscore.js 初探
    Normalize.css 初识
    什么是 HTML?
  • 原文地址:https://www.cnblogs.com/liuweiqiang11188/p/5357857.html
Copyright © 2011-2022 走看看