![](/img/trans.png)
[英]How to find cell indexPath that was selected when calling [tableView setEditing:NO]?
[英]How to find the indexPath of a custom cell, when the cell contains a specific item?
我有一個包含幾個項目的自定義單元格,包括名稱和年齡。
在另一個視圖控制器的viewDidLoad中,我使用特定的名稱等設置單元格。
在viewDidDisappear中,我想知道名稱“ X”的年齡。
但是,我完全沒有引用該單元格,並且我試圖找出包含名稱“ X”的單元格的indexPath是什么,以獲取其相應的年齡值。
我偶然發現了這一點:
let indexPath = NSIndexPath(forRow: <#Int#>, inSection: <#Int#>)
為了使用此:
let cell: customCellTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! customCellTableViewCell
這是我設置cellForRowATIndexPath的方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: customCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! customCellTableViewCell
let row = indexPath.row
let memb = self.arrayMemb[indexPath.row]
cell.setCell(memb.name, age: memb.age)
return cell
}
而且我將能夠繼續。 但是,我不知道如何基於名稱值“ X”獲取NSIndexPath。
任何想法如何執行,無限感謝,
如果知道名稱,請從數據源(模型)而不是從單元格(視圖)獲取成員
該代碼假定memArray
在任何情況下memArray
包含具有給定名稱的成員。
let givenName = "John Doe"
let filteredMembers = memArray.filter {$0.name == givenName}
let age = filteredMembers[0].age
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.