gpt4 book ai didi

arrays - 快速从 TableView 列表中删除多个项目

转载 作者:搜寻专家 更新时间:2023-11-01 06:06:35 25 4
gpt4 key购买 nike

在我检查了 tableview 列表中的多个项目后,我想将它们从我的列表中删除并将它们添加到收藏夹列表中。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//this is to make multiple selections in list
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark

//this gives me an array for rowsSelected
let rowsSelected = self.tableView.indexPathsForSelectedRows!.map{$0.row}
completeList.removeAtIndex(rowsSelected)

//tried this alternative - not sure what type "selection" is here
let selection = tableView.indexPathsForSelectedRows{
completeList.removeAtIndex(rowsSelected)
}

最佳答案

为了能够从您的 TableView 中删除项目,您应该开始反向删除。这样您遇到的问题就不会发生。例如,如果您想从数组 [1, 2,3,4,5,6]你应该从 6 到 2 开始删除。为了更清楚,你应该从最底部的单元格开始删除,这样索引就不会超出范围。

例子:

if let indexPaths = tableView.indexPathsForSelectedRows  {
//Sort the array so it doesn't cause a crash depending on your selection order.
let sortedPaths = indexPaths.sorted {$0.row > $1.row}
for indexPath in sortedPaths {

let count = array.count

var i = count-1

for i in stride(from: i, through: 0, by: -1) {

if(indexPath.row == i){

array.remove(at: i)

}
}
}
tableView.deleteRows(at: sortedPaths, with: .automatic)
}

关于arrays - 快速从 TableView 列表中删除多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35543552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com