kan*_*ris 1 arrays selection uitableview swift
在我的 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)
}
Run Code Online (Sandbox Code Playgroud)
为了能够从您的 tableview 中删除项目,您应该开始反向删除。这样您的问题就不会发生。例如,如果您想从数组中删除 2,4,6 [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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3580 次 |
| 最近记录: |