Галочка появляется на каждой ячейке при загрузке приложения - PullRequest
0 голосов
/ 09 февраля 2019

Когда я загружаю свое приложение, я вижу, что все ячейки отмечены галочкой.Я хочу, чтобы это было снято, чтобы начать с.

Кто-нибудь знает, почему это происходит из кода ниже?(Это единственный раздел контроллера представления, в котором упоминается вспомогательное оборудование для галочки.)

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

    if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
        } else {
            tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
            }
}

1 Ответ

0 голосов
/ 10 февраля 2019

Пожалуйста, смотрите ниже код.Вам нужно установить accessoryType по умолчанию в ячейке для Row, а затем в didSelectRow вы можете изменить его в соответствии с вашей логикой.Я создал простую модель, чтобы показать, как вы можете установить accessoryType по умолчанию при перезагрузке

import UIKit

struct CellInfo{
    var title:String
    var isSelected:Bool
}

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var myTV: UITableView!

var CellInfoArr = [
    CellInfo(title: "First Row", isSelected: false),
    CellInfo(title: "Second Row", isSelected: false),
    CellInfo(title: "Third Row", isSelected: false),
    CellInfo(title: "Fourth Row", isSelected: false),
    CellInfo(title: "Fifth Row", isSelected: false)
]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    myTV.delegate = self
    myTV.dataSource = self
    myTV.reloadData()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return CellInfoArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = CellInfoArr[indexPath.row].title
    if CellInfoArr[indexPath.row].isSelected == true{
        cell.accessoryType = .checkmark
    }else{
        cell.accessoryType = .none
    }
    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
        CellInfoArr[indexPath.row].isSelected = false
    } else {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        CellInfoArr[indexPath.row].isSelected = true
    }
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...