22.UITableViewCell根据Cell的内容自适应

UITableView是我们最常用的开发控件之一, 有了UITableView, 那就肯定会有UITableViewCell了, 但在Cell里面会有很多坑, 比如根据内容自动缩放, 下面让我们来看看怎么做吧


1.界面布局

1

2

3

4


2.自定义Cell

在这里面, 我们要自定义一个Cell的类

// 绑定第一个Cell样式的title 
@IBOutlet weak var title: UILabel! 

// 绑定第一个Cell样式的detail 
@IBOutlet weak var detail: UILabel! 

// 绑定第二个Cell样式的titleTwo 
@IBOutlet weak var titleTwo: UILabel! 

// 绑定第二个Cell样式的detailTwo 
@IBOutlet weak var detailTwo: UILabel!

3.实现代码

// 1.绑定TableView 
@IBOutlet weak var tableView: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // 2.设置TableViewCell的估计高度 
    tableView.estimatedRowHeight = 44 

    // 3.设置TableView每行的高度为自动适应 
    tableView.rowHeight = UITableViewAutomaticDimension
} 

// 4.设置多少组的Cell 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

// 5.设置多少个Cell 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 

// 6.设置Cell的数据源 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    // 6.1.实例化Cell 
    var cell = TableViewCellOne() 

    if indexPath.row != 3 { 
        // 6.2.获取对应的Identifier的TableViewCell 
        cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! TableViewCellOne 

        // 6.3.设置title的内容 
        cell.title.text = "用户名称: " 

        // 6.4.设置detail的内容 
        cell.detail.text = "我是大叔"

        // 6.5.返回设置好的Cell 
        return cell 
    } else { 
        // 6.7.获取对应的Identifier的TableViewCell 
        cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! TableViewCellOne 

        // 6.8.设置titleTwo的内容 
        cell.titleTwo.text = "个人介绍: " 

        // 6.9.设置detailTwo的内容 
        cell.detailTwo.text = "武侯区武饿哦uasodas空间阿卡家 萨卡说了句爱上可怜的价位哦亲我饿哦i代理静安寺来得及阿斯利康的就阿斯顿静安寺来得及阿斯顿今晚iuqwoeuoiasjas开讲啦思考逻辑阿斯利康就阿斯利康就啊失联客机啊失联客机阿斯顿进口拉丝奥斯丁" 
    } 

    // 6.10.返回设置好的Cell 
    return cell 
} 

// 7.设置Cell的点击方法 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // 7.1.取消tableViewCell的点击效果 
    self.tableView.deselectRowAtIndexPath(indexPath, animated: true) 
}

4.最终效果

0


项目工程地址: 链接: http://pan.baidu.com/s/1hqCQNQG 密码: vhpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于UITableViewCell中包含WebView的自适应高度问题,一般的解决方案是在webViewDidFinishLoad方法中计算WebView的高度,并更新UITableViewCell的高度。但是这种方法有时会出现计算不准确的情况,以及性能问题。 最新的解决方案是使用iOS 11中引入的UITableViewAutomaticDimension,结合约束自动布局来实现UITableViewCell自适应高度。具体步骤如下: 1. 在Storyboard或XIB中,设置UITableViewCell的约束,包括WebView的顶部、底部、左右两侧的约束,并将WebView的高度设置为大于等于0的值(可以是一个较小的值,比如10)。 2. 在tableView(_:cellForRowAt:)方法中,设置WebView的代理为当前的UITableViewCell,并在webView(_:didFinish:)方法中更新UITableViewCell的高度: ```swift func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { // 计算WebView的内容高度 webView.evaluateJavaScript("document.readyState") { (result, error) in if result != nil { webView.evaluateJavaScript("document.body.offsetHeight", completionHandler: { (height, error) in if let height = height as? CGFloat { // 更新UITableViewCell的高度 self.heightConstraint.constant = height self.setNeedsUpdateConstraints() self.updateConstraintsIfNeeded() self.layoutIfNeeded() self.delegate?.didUpdateHeight() } }) } } } ``` 3. 在tableView(_:estimatedHeightForRowAt:)方法中,返回一个估算的高度(可以是一个较小的值,比如100): ```swift func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 100 } ``` 4. 在tableView(_:heightForRowAt:)方法中,返回UITableViewAutomaticDimension: ```swift func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } ``` 注意:在使用约束自动布局的情况下,需要保证UITableViewCell的高度约束是完整的,即顶部和底部都有约束,否则自适应高度可能会出现问题。同时,在更新UITableViewCell的高度时,需要调用setNeedsUpdateConstraints、updateConstraintsIfNeeded和layoutIfNeeded方法,以保证约束的更新及时生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值