【swift,oc】ios开发中巧用自动布局设置自定义cell的高度

   ios开发中,遇到自定义高度不定的cell的时候,我们通常的做法是抽取一个frame类,在frame类中预算好高度,再返回。

但是苹果出来自动布局之后。。。春天来了!!来看看怎么巧用自动布局设置自定义cell的高度(以下代码都用swift实现,oc也是一样的,只是语法不一样)

   (1)自定义一个cell视图,可以用xib,也可以用storyboard中得动态cell,拖好控件,利用自动布局设置好约束

   (2)在cell类中新建一个对象方法(swift中的函数),传入数据模型,返回一个CGFloat,必须按照下面实现哦

///返回cell的高度
    func heightForCell(status:HJCStatus)->(CGFloat){
        
        //设置数据
        self.status = status
        //刷新布局
        self.layoutIfNeeded()
        //返回最最下方控件的最大Y值,就是高度啦
        return  CGRectGetMaxY(bottomView.frame)
    }

   (2.1)当tableview中好几个自定义cell 的时候,我们就要在cell类中定义一个类方法(类函数),来判断用哪个cell(只用一种cell的忽略此步骤)

1 ///返回cell的identifier
2     class func identifierOfcell(status:HJCStatus)->(String){
3         
4         if 条件一 {
5             return "homeCell"
6         }
7         return "retweetedCell"
8     }
9    

   (3)牛逼的方法来了。UITableViewDelegate中,有一个代理方法(函数)

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

          这个函数,是预估cell的高度,实现这个函数之后,会提高程序的效率哦。不信可以自己测试一下哈。。。我们可以随便返回一个数值

//预估行高
    override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 200
    }

   (4)关键的步骤来啦!。  UITableViewDelegate 的返回cell高度的方法(函数)

//准确行高
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        //取得数据
        let status = statuses![indexPath.row]//获得identifier
        let id = HJCHomeCell.identifierOfcell(status)
        //取出合适的cell
        let cell = tableView.dequeueReusableCellWithIdentifier(id)  as! HJCHomeCell
        //计算出行高
        let rowHeigth = cell.heightForCell(status)return rowHeigth
    }

以上四步之后,已经可以实现根据数据自动调整,其实就是在显示之前偷偷把页面预布局一下而已。。。但是作为一个装逼的程序员。。还可以继续优化一下哈

。以为回滚的时候,已经还的重复计算行高,要优化一下哈。。。用苹果自带的NSCache类即可

简单介绍一下NSCache吧

        1. 线程安全

        2. 使用根 NSMutableDictionary 非常类似

        3. 当内存不足的时候,系统会自动清理 NSCache

        4. 使用 NSCache 有一个注意事项:一旦缓存的对象被清理,需要能够重新计算,NSCache 中的对象是不稳定的

 

-------------开始撸代码-------------

先实例化一个NSCache对象

var rowHeightCache = NSCache()

然后取行高的时候如下,只是比上面多两步而已。。。

 1 //准确行高
 2     override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
 3         //取得数据
 4         let status = statuses![indexPath.row]
 5         
 6         //先从行高缓存中取
 7         if  let rowHeight = rowHeightCache.objectForKey(status.id) as? CGFloat{
 8              return rowHeight
 9         }
10         
11         //获得identifier
12         let id = HJCHomeCell.identifierOfcell(status)
13         
14         //取出合适的cell
15         let cell = tableView.dequeueReusableCellWithIdentifier(id)  as! HJCHomeCell
16         //计算出行高
17         let rowHeigth = cell.heightForCell(status)
18         
19         //缓存起来,下次直接取
20         rowHeightCache.setObject(rowHeigth, forKey: status.id)
21         return rowHeigth
22     }

    

   

          

原文地址:https://www.cnblogs.com/haojuncong/p/4512901.html