Arrays 将tableView中的项目与所选项目进行比较

Arrays 将tableView中的项目与所选项目进行比较,arrays,swift,Arrays,Swift,我正在从api获取项目,我想将它们与我的数组进行比较 例如:获取的项=[1,2,3,4] 我想查找项目[1,2] 并希望在表视图上设置复选标记 在cellForRowAt()中 我用过这个 if(alreadySelectedItems.count != 0) { if(dataModel.contains(alreadySelectedItems[indexPath.row])){ cell.accessoryType = .checkmark

我正在从api获取项目,我想将它们与我的数组进行比较

例如:获取的
项=[1,2,3,4]

我想查找项目
[1,2]

并希望在
表视图上设置复选标记

cellForRowAt()中

我用过这个

if(alreadySelectedItems.count != 0) {
        if(dataModel.contains(alreadySelectedItems[indexPath.row])){
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
    }

其中,
dataModel
是api中的项目列表,
alreadySelecteditems
是我的
selectedItems

我越来越

错误:索引超出范围

下面是我的tableView扩展函数

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return dataModel.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! CheckableTableViewCell
    cell.dataModel = dataModel[indexPath.row]

    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    selectedItems.append(dataModel[indexPath.row])

}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
{
    selectedItems.removeObject(object: dataModel[indexPath.row])
}
细胞是:

class CheckableTableViewCell: UITableViewCell
{
    var dataModel : String?
    {
        didSet
        {
            guard let model = dataModel else {return}
            textLabel?.text = model
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.tintColor = .primaryColor
    }

    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }

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

如果您的数组

extension Array where Element: Hashable {
    func sameElements(from other: [Element]) -> [Element] {
        let firstSet = Set(self)
        let otherSet = Set(other)
        return Array(firstSet.intersection(otherSet))
    }
}

在代码中,您正在检查
dataModel
中是否存在
selectedItems[indexPath.row]
,即

dataModel.contains(selectedItems[indexPath.row])
相反,它必须反过来,即

selectedItems.contains(dataModel[indexPath.row])
这就是它给出
索引越界的原因
异常

只需将
tableView(u:cellForRowAt:)
的定义更改为

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
    let dataModel = self.dataModel[indexPath.row]
    cell.dataModel = dataModel
    cell.accessoryType = self.selectedItems.contains(dataModel) ? .checkmark : .none //here..........
    return cell
}

您能用所有的tableView方法编辑您的问题吗?完成请检查是否有numberOfSections函数?否,只有number of itemstry添加此函数并返回1。你是否能看到tableView行?检查@PGDev answer我认为是正确的方法。不,我正在做的是,我来到一个视图,调用api在数组中追加字符串,然后使用这个数组创建tableView并在不同的数组中追加所选项,现在,当取消视图时,我将这些selectedItems带到上一个视图,现在用户思考并想要更改其选择,所以在didSelect上,我将这些选定的数组带到我的视图中,我再次选择了它们。调用api并填充tableView,但现在我有一些我以前选择的项目,所以我需要这些选择出现在屏幕上。作为一个复选标记,我选择了它们
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
    let dataModel = self.dataModel[indexPath.row]
    cell.dataModel = dataModel
    cell.accessoryType = self.selectedItems.contains(dataModel) ? .checkmark : .none //here..........
    return cell
}