Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableView中选择的重复行_Ios_Swift_Uitableview - Fatal编程技术网

Ios 在UITableView中选择的重复行

Ios 在UITableView中选择的重复行,ios,swift,uitableview,Ios,Swift,Uitableview,我正在制作一个带有动态表的应用程序,当我加载表中的所有值并运行该应用程序时,我将选择一个带有复选标记的行,然后向下滚动,并查看下面的另一行是否也带有复选标记。但是,它并没有将另一行的数据添加到我的数据中,它仍然显示为选中状态 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.deq

我正在制作一个带有动态表的应用程序,当我加载表中的所有值并运行该应用程序时,我将选择一个带有复选标记的行,然后向下滚动,并查看下面的另一行是否也带有复选标记。但是,它并没有将另一行的数据添加到我的数据中,它仍然显示为选中状态

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
    // let genresFromLibrary = genrequery.collections
    let rowitem = genresFromLibrary![indexPath.row].representativeItem
    cell.textLabel?.text = rowitem?.value(forProperty: MPMediaItemPropertyGenre) as? String

    // Configure the cell...
    // cell.textLabel?.text =

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let rowitem = genresFromLibrary![indexPath.row].representativeItem
    print(rowitem?.value(forProperty: MPMediaItemPropertyGenre) as! String)
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark//places the checkmark

    GenresWanted.append(rowitem!)
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .none
    ///we want to delete it from the array here
    let rowitem = genresFromLibrary![indexPath.row].representativeItem
    print("Removed: \(rowitem?.value(forProperty: MPMediaItemPropertyGenre) as! String)")
    if let index = GenresWanted.index(of: rowitem!)
    {
        GenresWanted.remove(at: index)
    }
}

上面是我的代码。我认为这可能与cellforRowAt有关,但我不确定。

您应该在table view cell类中这样做

class YourTableViewCell:UITableViewCell {
    //This method is called when you select/deselect the cell and also when you reuse a cell
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        self.accessoryType  = selected ? .checkmark : .none;
    } 

    // ... Your code
}
并从didSelect/didSelect方法中删除附件类型逻辑


注意:如果表视图单元格中没有附加任何类,您应该这样做以管理您的逻辑。

这背后的问题是单元格重复使用,因此您应该将其设置为
。在
cellForRow

var selected:IndexPath

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)

  if(selected.row == indexPath.row)
  {
     cell.accessoryType = .checkmark
  }
  else
  {
     cell.accessoryType = .none
  }


 }

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      selected = indexPath
      // here reload the cell at that indexPath
 }

您需要显示完整的
cellForRowAt
。您忽略了重要的部分—如何设置单元格的
accessoryType
。单元格将被重用。在
cellForRowAt
之外更改UI元素的状态时,此更改可能会重复使用该单元格。建议的方法是将所选状态保留在模型中,并在
cellForRow
中进行相应设置。在
did(De)中选择rowat
更新模型中的值并重新加载行。顺便问一下:为什么数据源数组(
genresFromLibrary
)是可选的呢?@Sh_Khan的答案应该可以解决这个问题。要记住的关键是,当您将单元格出列时,始终使用else部分。我这样做了,但现在当我选择行并显示复选标记时,我将向下滚动并向上滚动,这些行上不再有复选标记。如果我要选择多行,该怎么办?我能用一个全局数组跟踪吗?是的…………继续,在didSelect中追加,在didSelect中删除,然后重新加载,效果很好。非常感谢你的帮助!