在这里,当我从UITableView中选择特定的行时,我试图删除数组中选定一个对象之后的其余对象。
这是我的示例数组:
var myArray = ["rahul","rahib","suhail","alex","siya"]
如果我选择
myArray[2]
,则应删除"suhail"
之后的其余元素,并且最终数组必须为["rahul","rahib","suhail"]
。这是我的尝试,但只删除一个对象:
var selectedIndex = indexPath.row
myArray.remove(at: selectedIndex+1)
提前致谢!
最佳答案
将let
更改为var
以获得myArray
var myArray = ["rahul","rahib","suhail","alex","siya"]
使用
removeSubrange(_:)
方法从数组中删除剩余项myArray.removeSubrange(indexPath.row..<myArray.count)