如何在Swift中使用dequeueReusableCellWithIdentifier?

JuJ*_*oDi 22 uitableview ios swift

如果我取消注释

tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?)
Run Code Online (Sandbox Code Playgroud)

我收到了错误

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
Run Code Online (Sandbox Code Playgroud)

说的是 UITableView? does not have a member named 'dequeueReusableCellWithIdentifier'

如果我打开tableview然后错误消失了,但是在Objective-C中我们通常会检查单元格是否存在,如果不存在,我们会创建一个新单元格.在Swift中,由于提供的样板文件使用了let关键字并解开了一个可选项,因此如果它是零,我们就无法重新分配它.

在Swift中使用dequeueReusableCellWithIdentifier的正确方法是什么?

Mik*_*ard 40

您可以隐式地将参数解包到方法中,并且还可以转换结果 dequeueReusableCellWithIdentifier以提供以下简洁的代码:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell

    //configure your cell

    return cell
}
Run Code Online (Sandbox Code Playgroud)

  • 这也是我能解决的问题 - 主要区别在于使用`!`(用于隐式展开的可选)而不是函数定义中的`?`(只是指出那些没有立即注意到的人)区别).我还认为Apple在XCode 6中提供的样板也将用这个替换它们的定义. (3认同)

Zor*_*ayr 26

如果在加载表之前尚未在表视图中注册单元格类型,则可以使用以下命令获取单元实例:

private let cellReuseIdentifier: String = "yourCellReuseIdentifier"

// MARK: UITableViewDataSource

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier)
  if (cell == nil) {
    cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:cellReuseIdentifier)
  }
  cell!.textLabel!.text = "Hello World"
  return cell!
}
Run Code Online (Sandbox Code Playgroud)


iai*_*ain 5

您需要打开tableView变量,因为它是可选的

if let realTableView = tableView {
    let cell = realTableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
    // etc
} else {
    // tableView was nil
}
Run Code Online (Sandbox Code Playgroud)

或者你可以缩短它

tableView?.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
Run Code Online (Sandbox Code Playgroud)

在回答你关于'在Objective-C中我们通常会检查单元是否存在的问题,如果不存在我们创建一个新dequeueReusableCellWithIdentifier单元'时,总是返回一个单元格(只要你注册了一个类或一个此标识符的nib),因此您无需创建新标识符.


Vip*_*ini 5

在 Swift 3 和 Swift 4 版本中你只需使用这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath as IndexPath) as UITableViewCell
        cell.textLabel?.text = "cell number \(indexPath.row)."

        //cell code here

        return cell
    }
Run Code Online (Sandbox Code Playgroud)

在 Swift 2 版本中

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell

 cell.textLabel?.text = "cell number \(indexPath.row)."

        //cell code here

        return cell
    }
Run Code Online (Sandbox Code Playgroud)