当单元格包含特定项目时,如何找到自定义单元格的 indexPath?
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
包含一个在任何情况下都具有给定名称的成员。
let givenName = "John Doe"
let filteredMembers = memArray.filter {[=10=].name == givenName}
let age = filteredMembers[0].age
我有一个包含多个项目的自定义单元格,包括姓名和年龄。
在另一个视图控制器中,在 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
包含一个在任何情况下都具有给定名称的成员。
let givenName = "John Doe"
let filteredMembers = memArray.filter {[=10=].name == givenName}
let age = filteredMembers[0].age