【问题标题】:How to clear selections in uitableview and show it dynamically?如何清除uitableview中的选择并动态显示?
【发布时间】:2018-12-20 06:18:57
【问题描述】:

我选择了一些tableView 单元格并将其存储在UserDefaults 中,还显示了我选择的内容。现在添加一个清除按钮并取消选择所有选定的行并清空选定的行array,然后是table reloadAll()。它不是动态显示的吗?如何清除按钮操作中的选择并立即显示tableView而不进行选择?

  @IBAction func clearSelection(_ sender: Any) {

//        self.sellerTableView.reloadInputViews()
//
//        self.sellerTableView.deselectRow(at: , animated: true)

这个不行!

        if let aRow = self.sellerTableView.indexPathForSelectedRow {
            self.sellerTableView.deselectRow(at: aRow, animated: true)
        }
//        self.userSelectedSellers.removeAll()

        self.sellerTableView.reloadData()
    }

【问题讨论】:

  • 添加一些代码以获得适当的帮助或建议。你试过什么?
  • 代码够用吗? @Amit

标签: swift uitableview dynamic deselect


【解决方案1】:

这是从 tableView 中取消选择所有单元格的方法:

for cell in tableView.visibleCells {
    cell.setSelected(false, animated: true)
}

tableView.reloadData()

【讨论】:

  • 它不工作。我已将您的代码放入按钮(在 tableview 之外)操作中。还添加了表重新加载。
  • 是的,只需将此代码放在任何地方(例如在您的 IBAction 中)
【解决方案2】:

如上所述,您有一个包含多个 选择,您应该首先创建一个 array 来添加您选择的 indexPaths 然后你写 -

if indexArray.contains(indexPath.row) {
*// then set the table cell as selected*
}
else {
*// remove the table view selection*
}

您可以根据需要选择和取消选择表格单元格。喜欢改变颜色或打勾..任何想法..

然后你在 didSelect Delegate 中做这样的事情 -

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if indexArray.contains(indexPath.row) {
        if let indexForRemove = indexArray.index(of: indexPath.row) {
            indexArray.remove(at: iindexForRemove)
        }
    }
    else {
        indexArray.append(indexPath.row)
    }

// 现在重新加载您的表格视图--- 完成

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多