UITableView动态适配cell高度

UITabelView进阶

 

1.1动态适配cell高度(实现效果如下图)

 

 

实现原理:

tableView.estimatedRowHeight = Int//估算cell高度
tableView.AutomaticDimension //自动调整高度

 

1.1.1storyboard布局

实现label的约束和设置label的(Lines)显示行数为0(这样可以实现显示多行),标记lable的tag方便转化使用

     

 

1.1.2源码

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    //这里使用的是viewController,需要手动连接和实现代理和数据源协议
    var cellText = "hello world\nhello world\nhello world\nhello world\nhello world\nhello world\nhello world\n"
    
    
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //估算行高
        tableView.estimatedRowHeight = 60
        //自动调整行高
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)
        let label = cell.contentView.viewWithTag(100) as! UILabel
        label.text = cellText

        
        return cell
    }

 

转载于:https://www.cnblogs.com/jerry-q1/articles/6110869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值