arrays - UITableViewCell 删除然后所有复选标记 swift 消失

标签 arrays swift uitableview

我在网上搜索了我的问题,但对我的情况毫无帮助。这有点特别。

我的应用程序有一个 list 功能,一切正常但有这个问题。在 UITableViewCell 上添加一个 复选标记 并在之后删除其他单元格之一。所有复选标记 都将被删除。

我的想法是,删除某些内容时阵列和连接会出现问题。我的意思是数组中属性的“顺序”。我问过我的同事 (IT),但没有人能帮助我。

class ViewControllerChecklist: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var myTableView: UITableView!

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return (checklist.count)
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.textColor = UIColor(red: 4 / 255.0, green: 59 / 255.0, blue: 101 / 255.0, alpha: 1.0)
        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
        cell.textLabel?.text = checklist[indexPath.row]

        return cell
    }

    // checkmarks when tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark

        tableView.deselectRow(at: indexPath, animated: true)
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCellEditingStyle.delete {
            checklist.remove(at: indexPath.row)
            myTableView.reloadData()
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        myTableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        UITableViewCell.appearance().tintColor = UIColor(red: 237 / 255.0, green: 108 / 255.0, blue: 4 / 255.0, alpha: 1.0)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

选中的单元格 enter image description here

点击删除 enter image description here

单元格已删除 enter image description here

最佳答案

您可以试用这段代码。

存储选定的值并在 UITableView reloadData() 时间进行比较。

class ViewControllerChecklist: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var myTableView: UITableView!

    var selectedChecklist: [String] = []

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return (checklist.count)
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.textColor = UIColor(red: 4 / 255.0, green: 59 / 255.0, blue: 101 / 255.0, alpha: 1.0)
        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
        cell.textLabel?.text = checklist[indexPath.row]

        if selectedChecklist.contains(checklist[indexPath.row]) {
            cell.accessoryType = .checkmark
        }
        else{
            cell.accessoryType = .none
        }

        return cell
    }

    // checkmarks when tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        selectedChecklist.append(checklist[indexPath.row])
        tableView.deselectRow(at: indexPath, animated: true)
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let value = checklist.remove(at: indexPath.row)
            myTableView.reloadData()
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        myTableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        UITableViewCell.appearance().tintColor = UIColor(red: 237 / 255.0, green: 108 / 255.0, blue: 4 / 255.0, alpha: 1.0)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

关于arrays - UITableViewCell 删除然后所有复选标记 swift 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52947016/

相关文章:

xcode - Swift - 取决于 TableView 单元格的详细 View Controller

ios - 如何以编程方式使用原型(prototype)单元格删除 TableView 节标题

arrays - 在 Matlab 中使用 hist 计算出现次数

arrays - F# 数组中的负索引

ios - 在导航栏中插入图标​​/使 BarButtonItem 不可点击 Swift iOS

swift - macOS - 像 iOS 一样在 View Controller 之间转换,不要打开单独的窗口

javascript - 具有属性的深层复制 Javascript 数组

c# - C# 中是否可以使用基于堆栈的数组?

ios - Swift 配置自定义单元格

ios - 断言失败-[UITableView _endCellAnimationsWithContext]