【问题标题】:How to save checkmark after using search in SearchBar?在 SearchBar 中使用搜索后如何保存复选标记?
【发布时间】:2019-04-23 12:16:08
【问题描述】:

我正在尝试在 iOS 中创建一个搜索栏,并将其设置到过滤结果的位置,然后当您单击它时,它会显示一个复选标记。当我删除搜索文本时,复选标记消失并出现整页单元格,但我在搜索中选择的单元格未选择复选标记。这是通过 setSelected 方法在我的单元格中实现的:

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    
    if selected {
        accessoryType = .checkmark
    } else {
        accessoryType = .none
    }
}

我在 cellForRowAt 和 didSelectRowAt 方法中没有任何带有复选标记的逻辑。

Here is a GIF of my problem

有人可以帮我吗?

【问题讨论】:

  • 表格视图有两个数组吗?
  • @ZeeshanAhmed 不,只有一个
  • 你能显示 cellForRowAt 的代码吗?如果您以正常方式重用表格单元格,则不能使用单元格本身来跟踪选定状态等内容。屏幕外的单元格将不复存在,并可能被重新用于显示其他数据。您确实需要保留一个单独的表来为您的数据建模。从 ceelForRowAt 中的表中设置选中状态

标签: swift tableview uisearchbar checkmark


【解决方案1】:

我假设您当前所做的只是在您的单元格上设置复选标记。该单元格将被重复使用,您将失去复选标记。

您需要做的是跟踪项目,例如在Set 中,用户到目前为止已经检查过。在表格视图中渲染项目时,您需要查阅此设置是否选中该项目。如果是,您需要在单元格中添加复选标记。

Item 是您的细胞模型。这应该让你开始。您应该能够将其扩展到包含表标题组的数据集。

/// The items that have been checked.
var checkedItems = Set<Item>()

/// All items that are shown when no search has been performed.
var allItems: [Item]

/// The currently displayed items, which is the same as `allItems` in case no
/// search has been performed or a subset of `allItems` in case a search is
/// currently active.
var displayedItems: [Item]

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let item = displayedItems[indexPath.row]
    let cell = // Construct your cell as usual.

    if checkedItems.contains(item) {
        cell.accessoryType = .checkmark
    } else {
        cell.accessoryType = .none
    }

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let item = displayedItems[indexPath.row]

    if checkedItems.contains(item) {
        checkedItems.remove(item)
        cell.accessoryType = .none
    } else {
        checkedItems.insert(item)
        cell.accessoryType = .checkmark
    }
}

func updateSearchResults(for searchController: UISearchController) {
    displayedItems = // Set the displayed items based on the search text.
    tableView.scrollRectToVisible(.zero, animated: false)
    tableView.reloadData()
}

【讨论】:

    【解决方案2】:

    我这样解决了我的问题:

    var selectedCellIndex: String?
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let key = sectionTitles[indexPath.section]
        if let values = dictionary[key] {
            selectedCellIndex = values[indexPath.row]
        }
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.impactOccurred()
    }
    
    public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.tintColor = Color.main()
        cell.selectionStyle = .none
        let key = sectionTitles[indexPath.section]
        if let values = dictionary[key] {
            cell.textLabel?.text = values[indexPath.row]
            if values[indexPath.row] == selectedCellIndex {
                cell.accessoryType = .checkmark
            }
        }
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多