ios - 如何在 swift 3.0 中追加 didSelectRow 并删除 didDeselectRow

标签 ios arrays swift xcode

我正在尝试制作一个函数,将选定的行附加到数组,并在取消选择该行时从数组中删除。你们能帮我解决这个问题吗?

选择行函数:

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/

相关文章:

python - 如何从数据帧中获取数组或列表(其中 python 读取为 str 格式类型)?

c++ - 在 C++ 中实现 LRU

json - 使用泛型和 Decodable 解码 JSON 数组

ios - 存储少量具有 IAP 潜力的图像

ios - 条件语句中的 Objective-C 转换为自定义类

ios - 如何在UIBarbutton中将UIAlertcontroller实现为PopOver(向上箭头方向)

ios - UIPickerView.selectedRowInComponent 中的 "component"是什么意思?

iphone - NSManagedObjectContext 的 performBlockAndWait : not executing on the receiver's queue

php - 使用以下命令从表中的所有行中获取单元格的数据

ios - 可从所有文件访问 Swift 数组