xcode 如何引用cellForRowAtIndexPath之外的单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44363940/
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
How to refer to a cell outside of cellForRowAtIndexPath
提问by Dups
I am trying to update a label in a cell outside of
我正在尝试更新外部单元格中的标签
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
by using
通过使用
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Cell
in another function. However, I keep getting errors. Therefore, I tried to refer to the cell by using let cell = Cell()
, but I get the error unexpectedly found nil
. Lastly, I tried
在另一个函数中。但是,我不断收到错误消息。因此,我尝试使用 来引用单元格let cell = Cell()
,但出现错误unexpectedly found nil
。最后,我试过了
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! Cell
that does not return an error, but the label cell.time.text
is not updated.
这不会返回错误,但标签cell.time.text
不会更新。
回答by Harshal Valanda
Swift 3.0& Swift 4
斯威夫特 3.0和 斯威夫特 4
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath)
Swift 2.3
斯威夫特 2.3
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath)
NOTE :-With help of above method you can only get visible cell of tableView
except cell is nil
注意:-在上述方法的帮助下,您只能获得tableView
除了单元格为零的可见单元格
回答by Inder Jagdeo
If you want to use cell outside of the UITableView
override method cellForRowAt indexPath
then first of all, you need to fetch the indexPath
of row and then using the indexPath
of that row you can get the cell of UITableView
.
如果您想在UITableView
覆盖方法之外使用单元格,cellForRowAt indexPath
那么首先,您需要获取indexPath
行的 ,然后使用该indexPath
行的单元格可以获得UITableView
.
Once, you get the cell then you can access their properties & methods outside of UITableView
.
一次,您获得了单元格,然后您就可以在UITableView
.
Try this:
尝试这个:
In this code, i'm use a Custom Cell.
在这段代码中,我使用了自定义单元格。
let indexPath = IndexPath.init(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! MembersCell
回答by Thilina Chamin Hewagama
Step1:
第1步:
update the data of the model
更新模型数据
Step2:
第2步:
reload indexPath
重新加载索引路径
let indexPathToRefresh = IndexPath(row: 0, section: 0)
tableView.reloadRows(at: [indexPathToRefresh], with: .none)
回答by Micha? Kwiecień
Do not use dequeueReusableCellWithIdentifier
outside the cellForRowAtIndexPath
method.
不要dequeueReusableCellWithIdentifier
在cellForRowAtIndexPath
方法外使用。
You should use cellForRow(at:)
https://developer.apple.com/reference/uikit/uitableview/1614983-cellforrow
您应该使用cellForRow(at:)
https://developer.apple.com/reference/uikit/uitableview/1614983-cellforrow
回答by Vlad Pulichev
Use this:
用这个:
tableView.cellForRow(at: IndexPath(row: row, section: section)) as! CustomCell
If your cell has properties like
如果您的单元格具有类似的属性
@IBOutlet label: UILabel!
you can use this:
你可以使用这个:
tableView.cellForRow(at: IndexPath(row: row, section: section)) as! CustomCell).label.text = "Test123" // smth like this
Hope it helps
希望能帮助到你