表格视图单元格的动态高度会扩展,但是显示空视图。
创始人
2024-12-10 05:31:54
0

要解决表格视图单元格的动态高度会扩展但显示为空视图的问题,可以按照以下步骤进行操作:

  1. 确保表格视图的代理方法中已经正确实现了计算单元格高度的方法,并且返回了正确的高度值。

  2. 在单元格的布局代码中,设置自动布局约束以适应内容的动态高度。可以使用自动布局库如SnapKit或使用原生的autolayout属性。

  3. 在单元格的布局代码中,确保设置了内容视图的约束,以确保内容视图能够扩展以适应内容的高度。

  4. 如果单元格的内容是根据异步加载的数据来确定的,可以在加载数据完成后,调用表格视图的reloadData方法来刷新表格视图的布局。

以下是一个示例代码,展示如何解决表格视图单元格动态高度扩展但显示为空视图的问题:

class MyTableViewController: UITableViewController {
    var data: [String] = [] // 假设这是表格视图的数据源

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 100
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyTableViewCell
        cell.textLabel?.text = data[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

class MyTableViewCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupViews() {
        // 设置自动布局约束
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        contentView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        contentView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        contentView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

        // 设置内容视图的约束
        textLabel?.translatesAutoresizingMaskIntoConstraints = false
        textLabel?.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8).isActive = true
        textLabel?.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8).isActive = true
        textLabel?.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8).isActive = true
        textLabel?.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8).isActive = true
    }
}

在上面的示例代码中,MyTableViewController是表格视图的控制器,MyTableViewCell是表格视图的单元格。在viewDidLoad方法中,设置了表格视图的行高为自动维度,并且注册了单元格。在tableView(_:cellForRowAt:)方法中,设置了单元格的文本内容。在tableView(_:heightForRowAt:)方法中,返回了自动维度的行高。

MyTableViewCell中的setupViews方法中,设置了单元格内容视图的自动布局约束,确保内容视图能够扩展以适应内容的高度。

请注意,这只是一个示例代码,具体的实现可能会根据项目的需求而有所不同。

相关内容

热门资讯

iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...
安卓系统怎么连不上carlif... 安卓系统无法连接CarLife的原因及解决方法随着智能手机的普及,CarLife这一车载互联功能为驾...
安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
oppo手机安卓系统换成苹果系... OPPO手机安卓系统换成苹果系统:现实吗?如何操作?随着智能手机市场的不断发展,用户对于手机系统的需...
安卓平板改windows 系统... 你有没有想过,你的安卓平板电脑是不是也能变身成Windows系统的超级英雄呢?想象在同一个设备上,你...
安卓系统上滑按键,便捷生活与高... 你有没有发现,现在手机屏幕越来越大,操作起来却越来越方便了呢?这都得归功于安卓系统上的那些神奇的上滑...
安卓系统连接耳机模式,蓝牙、有... 亲爱的手机控们,你们有没有遇到过这种情况:手机突然变成了“耳机模式”,明明耳机没插,声音却只从耳机孔...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...