代码改变世界

UITableView从下往上加载cell CGAffineTransform

2018-09-18 11:08  法子  阅读(968)  评论(0编辑  收藏  举报

tableView是从上往下依次加载cell的。有时候想从下往上加载cell,比如聊天页面就是这样的,可以通过对tableView和其cell在y轴上缩放-1。来实现,当然,

tableView.contentOffset.y的值、indexPaht的值也因此是从底部往上增长的。

    override func viewDidLoad() {

        super.viewDidLoad()

 

        //默认值 CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0)。a:x轴缩放,d:y轴缩放,b:沿x拉伸,比如会把正方形拉伸成平行四边形,c:沿y拉伸,比如会把正方形拉伸成平行四边形,tx:沿x轴平移,ty:沿y轴平移

        tableView.transform = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: 0)

    }

 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        cell.transform = tableView.transform;

 

        return cell

    }