ios - 如何在 Swift 中创建动态大小的 UITableViewCell?

标签 ios swift uitableview

我正在尝试以编程方式创建一个具有动态大小和 View 的表。问题是自动高度不起作用。当我创建具有更多 View 的单元格时,如何确保单元格高度保持正确。

这是它的样子: Here is what it looks like:

这是我的代码:

import SnapKit
class FeedTableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 500
        tableView.allowsSelection = false
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("appDataCell", forIndexPath: indexPath) as! AppDataTableViewCell
        let view =  cell.inner
        cell.ivLogo.kf_setImageWithURL(NSURL(string: "http://creativosestrategicos.com/wp-content/uploads/2015/08/sample-logo-black16-300x58.png")!)
        cell.tvLabel.text = "Post Title"


        let label = UILabel()
        label.text = "Test"
        view.addSubview(label)
        label.snp_makeConstraints { (make) in
            make.top.equalTo(view)
            make.left.equalTo(view)
            make.right.equalTo(view)
        }

        return cell
    }
}

这是我的 Storyboard: Here is my storyboard

最佳答案

你应该覆盖高度方法

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100 /// 100 is your value here
    }

关于ios - 如何在 Swift 中创建动态大小的 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36771522/

相关文章:

swift - 如何打开 View Controller 的同一实例?

ios - Xcode 7 (7A220) 不再显示模拟器版本

ios - 使用 Alamofire 将图像上传到服务器不起作用

iphone - 一个具有多个 "File' 所有者的 xib 文件

ios - 从 View 中心向右动画 UIButton 的约束

swift - 如何使用 Swift 将 Parse 安装 objectId(或 devicetoken)保存为 cookie

iOS TableViewController 设置状态栏颜色

iphone - 如何确定 UISearchDisplayController 是否为 searchResults TableView 可见?

ios - 调用 [MagicalRecord setupCoreDataStackWithStoreNamed] 时的 SIGABRT

iphone - 由于僵尸对象,向下滚动会导致具有自定义 UITableViewCell 的 UITableView 上的应用程序崩溃