繁体   English   中英

当单元格包含特定项目时,如何查找自定义单元格的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在任何情况下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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM