在这里,当我从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)

07-24 09:51
查看更多