После удаления TableViewCell все галочки исчезают при быстром - PullRequest
0 голосов
/ 23 октября 2018

Я искал в сети свою проблему, но в моем случае ничего не помогло.Это что-то особенное.

Мое приложение имеет функцию контрольного списка, все работает нормально, но есть эта проблема.При добавлении checkmark на UITableViewCell и удалении одной из других ячеек после этого.Все checkmarks будут удалены.

Я думал, что у массива и соединения возникают проблемы, когда что-то удаляется.Я имею в виду «порядок» свойств в массиве.Я спросил своих коллег (ИТ), но никто не мог мне помочь.

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

Ответы [ 2 ]

0 голосов
/ 23 октября 2018

Более простое решение - сделать так, чтобы ваш список объектов CheckList добавлял isChecked в свой список проверок.

struct Object {
    var tag:String
    var isChecked:Bool
}

И создавал список следующим образом:

var checkList = [Object]()

наконец в функции cellForRowAt:

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

    //  rest of code goes here...

    if checklist[indexPath.row].isChecked {
        cell.accessoryType = .checkmark
    }
    else{
        cell.accessoryType = .none
    }

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    checkList[indexPath.row].isChecked = true
    tableView.deselectRow(at: indexPath, animated: true)
}
0 голосов
/ 23 октября 2018

Вы можете попробовать этот код.

Сохранение выбранного значения и сравнение в UITableView reloadData () time.

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()
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...