如何正确地将代码中的字幕 UITableViewCell 出列?

Posted

技术标签:

【中文标题】如何正确地将代码中的字幕 UITableViewCell 出列?【英文标题】:How to dequeue subtitle UITableViewCell in code properly? 【发布时间】:2017-01-11 21:35:55 【问题描述】:

我正在尝试使用纯代码中的常规、非自定义 .subtitle 单元格创建 UITableView。但是,下面的代码从来没有给我一个正确的detailTextLabel 单元格,而是选择了一个.default 单元格。

public var cellIdentifier: String  return "wordsCell" 
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell: UITableViewCell
    if let newCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) 
        // Always succeeds, so it never goes to the alternative.
        cell = newCell
    
    else 
        // This is never reached.
        cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
    

    let word = wordAtIndexPath(indexPath: indexPath)
    cell.textLabel?.text = word.text
    cell.detailTextLabel?.text = word.subText

    return cell

这显然是因为dequeueReusableCell(withIdentifier:) 实际上并没有返回nil,即使当前没有可用的单元格。相反,如果没有创建单元格,它总是返回 .default

另一个选项,dequeueReusableCell(withIdentifier:for:) 也总是成功,所以这也行不通。

到目前为止,如果没有 Interface Builder 来定义原型单元格的样式,在纯代码中创建非.default 样式的单元格似乎是不可能的。我能想到的最接近的是answer,它注意到了同样的问题。我发现的所有其他问题也涉及 IB 问题或自定义单元格。

有谁知道如何在不使用 Interface Builder 的情况下将 .subtitle 单元格从表格视图中出列?

【问题讨论】:

可以交换条件表达式的顺序吗? @NRitH 可能不会,因为我们只想在必要时初始化一个新单元格。如果我交换条件,它将总是初始化一个新单元格。 您能解释一下您要达到的目标吗?如果是哪种情况你想要字幕? @mat 我一直想要字幕。但是,不使用 Interface Builder。 您是否首先使用func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)在 viewDidLoad 中注册单元格 【参考方案1】:

我测试了它并且它有效。我以为你想继承 UItableViewCell,但在这种情况下你不必注册单元格。

class TableViewController: UITableViewController 

    let wordAtIndexPath = ["one", "two", "three"]
    let cellId = "cellId"
    override func viewDidLoad() 
        super.viewDidLoad()

    

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int 
        return 1
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        // #warning Incomplete implementation, return the number of rows
        return wordAtIndexPath.count
    


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

        let word = wordAtIndexPath[indexPath.row]
        let cell: UITableViewCell = 
            guard let cell = tableView.dequeueReusableCell(withIdentifier: cellId) else 
                return UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: cellId)
                
            return cell
        ()

        cell.textLabel?.text = "My cell number"
        cell.detailTextLabel?.text = word
        return cell
    


【讨论】:

太棒了。看到就清楚,但之前不明显【参考方案2】:

我认为您的问题可能是您在界面生成器(或viewDidLoad)中注册了一个与“cellIdentifier”同名的单元格。 如果要使用字幕类型单元格,则不应注册任何单元格。通过注册一个单元格,它将首先尝试创建该单元格(不会是字幕类型的单元格)。

【讨论】:

除了我没碰过的原始storyboard,没有Interface Builder文件。因此,任何单元格都没有机会具有该标识符。为了清楚起见,我添加了单元格标识符。 您不应该注册任何单元格,如果您删除该代码,它应该可以工作。通过注册一个单元格,它将首先尝试创建该单元格(不会是字幕类型的单元格)。 成功了!您可以创建一个答案,以便我接受吗? 太棒了!我已经更新了答案,所以它还提到在viewDidLoad 中注册一个单元格。【参考方案3】:

我遇到过类似的问题,发现如果您不使用情节提要,最优雅的方法是继承 UITableViewCell。并使用该自定义类将单元格出列。

两步:

    创建字幕单元格:

     class DetailCell: UITableViewCell 
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) 
     super.init(style: .subtitle, reuseIdentifier: "reuseIdentifier")
    
    
    
     required init?(coder: NSCoder) 
     super.init(coder: coder)
     
    
    
    

    出列单元可选择将其转换为该类:

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
     guard let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as? DetailCell else 
         return UITableViewCell.init(style: .subtitle, reuseIdentifier: "reuseIdentifier")
     
    
     // Configure the cell...
     cell.textLabel?.text =  "Title"
     cell.detailTextLabel?.text = "Subtitle"
     return cell
    
     
    

【讨论】:

以上是关于如何正确地将代码中的字幕 UITableViewCell 出列?的主要内容,如果未能解决你的问题,请参考以下文章

如何正确地将 UIButton 中的图像放入图像中?

如何正确地将指针传递给第 3 方代码的成员函数? [复制]

如何正确地将值从 pyqt 返回到 JavaScript?

如何正确地将旧日期插入表格?

如何正确地将 blob 从前端传递到后端?

如何正确地将日期时间从 c# 传递到 sql?