【问题标题】:How to remove item from Array?如何从数组中删除项目?
【发布时间】:2018-02-13 14:21:39
【问题描述】:

我有一个多选的表格视图。

我在做什么:当用户选择项目时,这些项目会附加到数组中。

当用户从单元格中取消选择项目时,这个取消选择的项目从数组中删除。

我做了什么:

我的数组var selectedTagList:[Tag] = []

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tagTableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        self.selectedTagList.append(tagList![indexPath.row])

    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        self.tagTableView.cellForRow(at: indexPath)?.accessoryType = .none
        self.selectedTagList.remove(at: indexPath.row)
    }

请提供任何建议或示例代码?

//数据源和委托

extension PickVideoViewController : UITableViewDataSource,UITableViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let tlist = self.tagList , !tlist.isEmpty else { return 1}
        return tlist.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let tlist = self.tagList , !tlist.isEmpty else {
            let cell = UITableViewCell()
            cell.selectionStyle = .none
            cell.backgroundColor = .clear
            cell.textLabel?.textAlignment = .center
            cell.textLabel?.textColor = UIColor.black
            cell.textLabel?.text = "nodataavaiable".localized()
            return cell }
         let cell = tableView.dequeueReusableCell(withIdentifier: "TagCell", for: indexPath) as! TagCell
         cell.tagName.text = tlist[indexPath.row].tag
         cell.accessoryType = cell.isSelected ? .checkmark : .none
         cell.selectionStyle = .none // to prevent cells from being "highlighted"
         return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tagTableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        self.selectedTagList.append(tagList![indexPath.row])

    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        self.tagTableView.cellForRow(at: indexPath)?.accessoryType = .none
        self.selectedTagList.remove(at: <#T##Int#>)
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        guard let tlist = self.tagList , !tlist.isEmpty else {return tableView.frame.height }
        return 40

    }

}

【问题讨论】:

  • 那么问题是什么?
  • 问题是当 ı remove : out of bounds
  • 你能展示你的 UITableViewDataSource 吗?
  • 如果您选择第 10 个单元格,这是您的第二个选择。该数组将仅包含 2 个项目。您正试图在索引 9 处删除。这是超出范围的。
  • 如果将数字 7 和数字 9 插入到数组中,它将有两个项目。然后您尝试删除第 7 项。看到这里的问题了吗?

标签: swift uitableview swift4


【解决方案1】:
self.selectedTagList.remove(at: indexPath.row)

indexPath.row 是要删除的错误值。由于您的数据源基于tagList 而不是selectedTagList 填充,因此您需要从tagList 中取出项目并在selectedTagList 中找到等效项目。

您没有显示tagList 中的对象类型,但您可能需要使它们符合Equatable,以便您可以进行此查找。一旦你这样做了,你应该有这样的东西:

let deselectedTag = self.tagList[indexPath.row]
// You will need the items in `tagList` to conform to `Equatable` to do this
guard let indexOfDeselectedTag = self.selectedTagList.index(of: deselectedTag else) {
    // Data inconsistency: the item wasn't found in selectedTagList
    return
}
self.selectedTagList.remove(at: indexOfDeselectedTag)

【讨论】:

    【解决方案2】:

    您不需要维护所选项目的列表。您已经拥有了所有项目,并且 tableView 可以告诉您选择了哪些行/项目。

    https://developer.apple.com/documentation/uikit/uitableview/1614864-indexpathsforselectedrows

    讨论

    这个属性的值是一个index-path数组 每个对象都通过其部分和行索引标识一行。这 如果没有选定的行,则此属性的值为 nil。

    您正在尝试重新发明轮子。始终检查文档以了解现有功能。

    如果您想要从中选择项目的列表,您只需在这些索引路径处创建一个项目数组,如下所示:(未经测试)

    let selectedItems = tableView.indexPathsForSelectedRows().map {  
         self.tagList[$0.row] 
    }
    

    此代码将遍历索引路径并在每个索引路径中返回数组中的项目。 (这是未经测试的,您可能需要在更改类型时使用 flatMap)

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2016-09-07
      • 2016-10-30
      • 2021-08-15
      • 1970-01-01
      • 2021-10-03
      • 2014-09-21
      相关资源
      最近更新 更多