ios 如何在 Swift 中使用 dequeueReusableCellWithIdentifier?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24471954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:09:40  来源:igfitidea点击:

How to use dequeueReusableCellWithIdentifier in Swift?

iosuitableviewswift

提问by JuJoDi

If I uncomment

如果我取消注释

tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?)

I get an error on the line

我在线时出错

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

that says UITableView? does not have a member named 'dequeueReusableCellWithIdentifier'

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

If I unwrap the tableview then the error goes away, but in Objective-C we would normally check whether or not the cell exists, and if it does not we create a new one. In Swift, since the boilerplate provided uses the letkeyword and unwraps an optional, we can't reassign it if it's nil.

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

What's the proper way to use dequeueReusableCellWithIdentifier in Swift?

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

回答by Mike Pollard

You can implicitly unwrap the parameters to the method and also cast the result of dequeueReusableCellWithIdentifierto give the following succinct code:

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

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

    //configure your cell

    return cell
}

回答by Zorayr

If the cell type hasn't been registered with the table view before the table loads, you can use the following to fetch a cell instance:

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

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!
}

回答by iain

You do need to unwrap the tableView variable as it is an optional

您确实需要解开 tableView 变量,因为它是可选的

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

or you can shorten it by

或者你可以缩短它

tableView?.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

In answer to your question about 'in Objective-C we would normally check whether or not the cell exists, and if it does not we create a new one', dequeueReusableCellWithIdentifieralways returns a cell (so long as you've registered a class or a nib for this identifier), so you don't need to create a new one.

为了回答您关于“在 Objective-C 中我们通常会检查单元格是否存在,如果不存在我们创建一个新dequeueReusableCellWithIdentifier单元格”的问题,总是返回一个单元格(只要您注册了一个类或一个这个标识符的笔尖),所以你不需要创建一个新的。

回答by Vipin Saini

In Swift 3 and Swift 4 Version you just use this

在 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
    }

In Swift 2 Version

在 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
    }

回答by bazik

swift 3 version:

快速 3 版本:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath!) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier:"CellIdentifier", for: indexPath) as UITableViewCell
      return cell
    }