如何使用UitaiteViewCell存储隐藏的字符串



我想将信息(字符串)与uitaiteViewCells填充tableView时,将信息(字符串)附加到每个UITATION VIEWCELL。我不希望看到它 - 选择行/单元格时,我只需要访问此字符串即可。什么是最好的方法?

您的最佳选择可能是子类UITableViewCell并添加字符串属性。然后可以将其分配为常数,或者在cellForRowAtIndexPath:中设置单元格时。

class CustomCell: UITableViewCell {
    var customString = "Default"
}
--------
func cellForRow(atIndexPath indexPath: IndexPath) -> UITableViewCell {
    guard let cell: CustomCell = tableView.dequeueReusableCell(withReuseIdentifier: "identifier", indexPath: indexPath) as? CustomCell else {
        return UITableViewCell()
    }
    cell.customString = "String"
    return cell
}
func didSelectRow(atIndexPath indexPath: IndexPath) {
    guard let cell: CustomCell = tableView.cellForRow(atIndexPath: indexPath) as? CustomCell else {
        return
    }
    let stringFromCell = cell.customString
}

上面尚未编译,因此可能无法使用单词来工作,但是您得到了原则。

  • 将属性selected添加到您的模型中。
  • cellForRow中,根据selected的状态显示/隐藏字符串。
  • didSelectRow中切换selected,然后重新加载行。

最新更新