如何在使用 tableview 和搜索栏时添加复选标记

How to add checkmarks when using tableview and a searchbar

我有一个搜索栏可以过滤我的数组。几天来我尝试做的是在点击单元格时添加复选标记。我知道如何添加复选标记,但我不知道如何在搜索和不搜索时使复选标记保留在同一个单元格中。

现在我正在尝试制作一个显示所有选中行的数组:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    if schouldShowSearchResults{
        if checkedArrray.contains(filteredData[indexPath.row]){
            checkedArrray.remove(at: checkedArrray.index(of: filteredData[indexPath.row])!)
        }else{
            checkedArrray.append(filteredData[indexPath.row])
        }
    }else{
        if checkedArrray.contains(array[indexPath.row]){
            checkedArrray.remove(at: checkedArrray.index(of: array[indexPath.row])!)
        }else{
            checkedArrray.append(array[indexPath.row])
        }
    }

    print(schouldShowSearchResults)
    print(checkedArrray)
}

CellforRowAt:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = serialNumberTableView.dequeueReusableCell(withIdentifier: "serialNumbers") as! SerialNumberCell

    if schouldShowSearchResults{
        cell.serialNumberLabel.text = filteredData[indexPath.row]
    }else{
        cell.serialNumberLabel.text = array[indexPath.row]
    }

    return cell
}

所以我现在想知道如何为所有与我的 checkedArray 具有相同文本值的单元格添加复选标记?

最好的办法是在 didSelect 方法的底部调用 self.tableView.reloadData()

然后,在每次重新加载 table 时都会调用的 cellForRow 方法中,您只需查看该单元格的文本值是否等于您想要的 checkedArray(即如果它应该显示复选标记)

最后,您的单元格可以有一个 UIImageView 形式的复选标记,如果文本值相等,则在 isHidden true 或 false 之间切换,如上所述。

不要使用单独的数组来跟踪复选标记,而是将 Bool 字段添加到您的数据模型。这样,是否应检查一行的状态与您的数据模型相关联,而不是单独的数组。

这使得过滤和排序您的 table 视图数据变得更加简单,而无需尝试使其他单独的数据保持同步。

然后在您的 didSelectRowAt 方法中,您应该获取给定 indexPath 的数据模型,切换其 selected(或 checked)属性,然后告诉table 查看以重新加载那个 indexPath。这使您的 didSelectRowAt 中的代码更加简单和清晰。

经过几天的研究,我发现使用结构数组是最好的。我制作了两个阵列,一个经过过滤,一个正常。

struct filtered{
    var text_filtered: String
    var isChecked_filtered: Bool = false
}

struct normal{
    var text: String
    var isChecked: Bool = false

}

在我的 didSelectRowAt 函数中,我是这样做的:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                tableView.deselectRow(at: indexPath, animated: true)

                if schouldShowSearchResults{
                    if filteredDataArray[indexPath.row].isChecked == false{
                        filtered

                    filteredDataArray[indexPath.row].isChecked = true
                    let indexOfArray = array.index { [=11=].text == filteredDataArray[indexPath.row].text_filtered}
                    array[indexOfArray!].isChecked = true
                    print(filteredDataArray[indexPath.row])
                }else{
                    filteredDataArray[indexPath.row].isChecked = false
                    let indexOfArray = array.index { [=11=].text == filteredDataArray[indexPath.row].text_filtered}
                    array[indexOfArray!].isChecked = false
                    print(filteredDataArray[indexPath.row])
                }


            }else{
                if array[indexPath.row].isChecked == false{
                    array[indexPath.row].isChecked = true

                    print(array[indexPath.row])
                }else{
                    array[indexPath.row].isChecked = false
                    print(array[indexPath.row])
                }


            }

            tableView.reloadData()

            }

之后我也遇到了一个问题。原来我的单元格是可重复使用的,所以每次我在 tableView 中滚动时,我的复选标记都会重新排序。我通过在 cellForRowAt 中这样做解决了这个问题:

    let cell = tableView.dequeueReusableCell(withIdentifier: "defaultCell") as! customCellClass

    if schouldShowSearchResults{
    cell.accessoryType = filteredDataArray[indexPath.row].isChecked ? .checkmark : .none
    cell.stringLabel.text = filteredDataArray[indexPath.row].text_filtered
    }else{
    cell.accessoryType = array[indexPath.row].isChecked ? .checkmark : .none
    cell.stringLabel.text = array[indexPath.row].text
    }
    return cell