【问题标题】:Trying to remove an element from an array i get this error: Contextual type 'Int' cannot be used with array literal尝试从数组中删除元素时出现此错误:上下文类型“Int”不能与数组文字一起使用
【发布时间】:2018-03-01 12:13:58
【问题描述】:

我在 tableView 中为 reloadCell 添加了一个按钮,我想在按下按钮时删除特定单元格上的数组元素,但我收到此错误

上下文类型 'Int' 不能与数组字面量一起使用

@IBAction func reloadCell(_ sender: UIButton) {

        let index = IndexPath(row: sender.tag, section: 0)
        sortedArray.remove(at: [index])    //HERE I GET THE ERROR
        self.tableView.reloadRows(at: [index], with: .right)
    }

我该如何解决?

【问题讨论】:

  • sortedArray.remove(at: sender.tag)
  • 你需要整数而不是 IndexPath 类

标签: ios arrays swift uitableview swift4


【解决方案1】:

数组使用 Ints 作为索引,而不是像 tableView 那样使用 IndexPaths:

sortedArray.remove(at: sender.tag)

【讨论】:

    【解决方案2】:

    你不能对数组使用 IndexPath 类型(你应该使用 int 类型) 例如:

            sortedArray.remove(at: index.row) 
            self.tableView.reloadRows(at: index.row, with: .right)
    

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多