swift 3 中的 cellForRow(at: IndexPath) 在 XCode 8 中表现得很有趣
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39951812/
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
cellForRow(at: IndexPath) in swift 3 is acting funny in XCode 8
提问by kylar13
I'm trying to recover a cell from a TableView using the method cellForRow(at: IndexPath) but the compiler doesn't seem to like this method because it won't let me call it :S
我正在尝试使用 cellForRow(at: IndexPath) 方法从 TableView 中恢复单元格,但编译器似乎不喜欢这种方法,因为它不允许我调用它:S
http://imgur.com/aVhpXr5(Can't post images yet)
http://imgur.com/aVhpXr5(还不能发布图片)
Here's the snippet of the code in question:
这是有问题的代码片段:
if let i = self.data.index(of: snapshot.value as! String) {
self.data[i] = snapshot.value as! String
self.tableView.cellForRow(at: i).textLabel!.text = "Modd"
self.tableView.cellForRow(at: [IndexPath(row: i, section: 0)])!.textLabel!.text = "mod"
}
else {
print("Modified an item that wasn't in the data list")
}
How should I be calling the method if the compiler accepts neither of the two?
如果编译器不接受这两者,我应该如何调用该方法?
回答by Carien van Zyl
Your problem with your second statement is that you add the indexPath that you create to an array.
您的第二个语句的问题是您将创建的 indexPath 添加到数组中。
Use the following code:
使用以下代码:
self.tableView.cellForRow(at: IndexPath(row: i, section: 0))
回答by zombie
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell:UITableViewCell
if let i = self.data.index(of: snapshot.value as! String) {
self.data[i] = snapshot.value as! String
cell = tableView.cellForRow(at: indexPath)!
cell.textLabel!.text = "Modd"
}
else {
print("Modified an item that wasn't in the data list")
}
return cell == nil ? UITableViewCell() : cell
}