表格视图,其中的单元格具有固定高度和动态高度。
创始人
2024-12-10 06:31:15
0

要实现具有固定高度和动态高度的表格视图,可以使用UITableView,并设置单元格的高度属性。

以下是一个示例代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    let tableView = UITableView()
    var data: [String] = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
        tableView.delegate = self
        
        // 设置tableView的frame,使其填充整个视图
        tableView.frame = view.bounds
        
        // 注册自定义的UITableViewCell类
        tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cell")
        
        // 设置tableView的行高为固定值
        tableView.rowHeight = 50
        
        // 设置tableView的估算行高为动态值
        tableView.estimatedRowHeight = 100
        
        // 允许自动调整行高
        tableView.rowHeight = UITableView.automaticDimension
        
        // 将tableView添加到视图中
        view.addSubview(tableView)
    }
    
    // UITableViewDataSource方法,返回行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    // UITableViewDataSource方法,返回每行的单元格
    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
    }
}

class MyTableViewCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 设置单元格的布局和外观
        setupCell()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupCell() {
        // 添加自定义的子视图和约束
        // 这里可以根据需求添加内容,比如添加图片、标签等
    }
}

在上述代码中,我们创建了一个UIViewController,并在其中创建了一个UITableView。我们设置了UITableView的dataSource和delegate为ViewController本身,并注册了自定义的UITableViewCell类。

在viewDidLoad方法中,我们设置了UITableView的行高属性为固定值,并设置了估算行高为动态值。我们还允许自动调整行高。

在tableView(_:cellForRowAt:)方法中,我们创建了自定义的UITableViewCell,并将数据赋值给单元格的textLabel。

通过这样的设置,UITableView中的单元格将具有固定高度和动态高度。如果内容超过了固定高度,单元格的高度将自动调整以适应内容。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...