ios - 如何为特定单元格分配标签?

标签 ios swift3 uitableview

现在,当我在单元格上向左滑动以显示“收藏夹”按钮并单击它时,“收藏夹”标签会出现在随机单元格上,而不是我滑动的单元格上。当我点击指定按钮时,如何使“收藏夹”标 checkout 现/消失在右侧单元格中?

现在我在 editActionsForRowAt 函数中的代码是:

internal func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

    let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
        cell.favoriteLabel.text = "Favorite"
        tableView.reloadRows(at: [indexPath], with: .none)
    }
    favorite.backgroundColor = UIColor.orange

    return [favorite]
}

问题是设置“favoriteLabel”=“Favorite”不会将右侧单元格的“favoriteLabel”设置为“Favorite”

最佳答案

你在这里采取了错误的方法。您应该更新数据模型,然后在索引路径处重新加载单元,而不是直接更新单元。然后您的 cellForRowAt 将根据更新的数据模型正确显示更新的单元格。

您实际上将两者奇怪地结合在一起。您(错误地)尝试获取单元格,然后设置其标签,然后重新加载该行。不要两者都做。只需更新您的数据模型,然后重新加载该行。就是这样。

顺便说一句,永远不要在 cellForRowAt: 之外调用 dequeueReusableCell。如果您坚持当前的方法(滚动后可能无法正常工作),则需要将对 dequeueReusableCell 的调用替换为对 cellForRow(at:) 的调用.

关于ios - 如何为特定单元格分配标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40822143/

相关文章:

ios - 在 firestore 中添加新 key 时应用程序崩溃

ios - 消除 UINavigationBar 上栏左侧和 leftBarButtonItem 之间的空间

ios - Google map ,使用 DispatchQueue 加载标记

swift - 用动画交换 rootViewController?

ios - UITableView 在有数据之前没有弹出数据

ios - 当 Leaks 工具不显示内存泄漏时如何调试它们?

ios - AVPlayerViewController 问题

swift - Swift 3 中带有选项的二元运算符

iphone - 根据内容更新 UITableViewCell 的编辑样式

ios - tableView 中的自定义分隔符