我正在尝试制作一个函数,将选定的行附加到数组,并在取消选择该行时从数组中删除。你们能帮我解决这个问题吗?
选择行函数:
var exercises = [exercise]() // Array that will be filled by selected rows
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath) as? exerciseCell
let exercisesSelected = selectedCell?.exerciseNameLbl.text
exercisenames.insert(exercisesSelected!, at: 0)
}
这是我的取消选择委托(delegate)方法:
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath) as? exerciseCell
if exercisenames.count < 1 {
print("empty array")
}else{
exercisenames.remove(at: indexPath.row)
print(exercisenames)
}
}
期待任何帮助,谢谢!
凯文。
最佳答案
您可以使用 for in
循环来删除数组中的特定对象。
for (index,value) in exercisenames.enumerate() {
if value == selectedCell.exerciseNameLbl.text {
exercisenames.remove(at:index)
}
}
关于ios - 如何在 swift 3.0 中追加 didSelectRow 并删除 didDeselectRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40110654/