我想将信息(字符串)与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
,然后重新加载行。