ios - 展开时注册表格单元格为零

标签 ios swift uitableview

我正在尝试在容器 View 中创建一个 TableView ,这样我就可以为它设置动画,这与常规的 UITableViewController 不同。

在 Storyboard 中创建了一个 UIViewController,其中一个 UIView 作为“containerView”。 containerView 里面是一个tableView。 IDK为什么它一直给我零。

class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self

        tableView.register(SearchViewCell.self, forCellReuseIdentifier: "SearchResult")
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SearchResult", for: indexPath) as! SearchViewCell
        cell.addressLabel.text = "Hello"
        cell.subtitleLabel.text = "There"
        return cell
    }   
}

搜索 View 单元格

class SearchViewCell: UITableViewCell {
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var subtitleLabel: UILabel!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

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

最佳答案

1-这个寄存器

tableView.register(SearchViewCell.self, forCellReuseIdentifier: "SearchResult")

仅对以编程方式创建的自定义单元格有效(显然是没有 socket 的单元格)

2- 对于原型(prototype)单元格(当您在布局中的表格内创建单元格时),那么您不需要寄存器

3- 如果你在 xib 中创建了单元格布局,那么还有另一种注册方式

 tableView.register(UINib(nibName: "SearchViewCell", bundle: nil), forCellReuseIdentifier: "cellID")

关于ios - 展开时注册表格单元格为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067682/

相关文章:

iphone - 有关在 iPhone 上使用 Google GData API - Youtube 的任何示例/教程吗?

ios - Swift:使用变量来描述 UIViews

ios - 使用像素数据从 SKTexture 创建 SKSpriteNode

ios - 将 tableViewCell 中的数据传递给另一个 VC 失败

ios - xcode ios - 每当调整 slider 时更新计划计时器

ios - 如何将值从一个 View Controller 传递到另一个 swift?

ios - 如何在 Swift 中继承 UITableViewController

javascript - 从 Objective-C 将大数据传递给 UIWebView javascript

ios - ScrollView 与 AutoLayout 不工作

ios - 如何使用这个格式化程序?