Tableview选中标记错误的地方

时间:2018-02-24 13:16:08

标签: swift uitableview checkmark

我试过这样在单元格上添加checkMark但是如果我点击第0行第0行, 第2节第0行也进行了检查。此外,如果点击第0行第1行,第3行第0行检查(如果我点击第2行第0行,则为vise verse, 如果点击第3行第0行,第0行第1行,则第0行第0行也被检查。

如果快速滚动,所有检查都会消失。 我怎样才能解决这个问题?提前谢谢!

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

        @IBOutlet var tableView: UITableView!

        var sectionName = ["Cars", "Motor Cycles", "EV", "Hybrid"]
        var cars = ["TOYOTA", "AUDI", "BMW", "HONDA", "VOLVO", "VW","FIAT"]
        var motorCycles = ["YAMAHA", "HONDA", "DUCATI", "HARLEY DAVIDSON", "VICTORY"]
        var yes = "YES"

        override func viewDidLoad() {
            super.viewDidLoad()

            tableView.dataSource = self
            tableView.delegate = self

        }



        func numberOfSections(in tableView: UITableView) -> Int {
            return sectionName.count
        }

        func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
            return sectionName[section]
        }

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

            if section == 0 {
                return cars.count
            } else if section == 1 {
                return motorCycles.count
            } else {
                return 1
            }

        }

        let cellId = "cell"

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

            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
            let section = indexPath.section

            if section == 0 {
                cell.textLabel?.text = cars[indexPath.row]
            } else if section == 1 {
                cell.textLabel?.text = motorCycles[indexPath.row]
            } else {
                cell.textLabel?.text = yes
            }

            return cell
        }

        var firstArray = [String?]()
        var secondArray = [String?]()
        var thirdString = ""
        var fourthString = ""

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

            let currentCell = tableView.cellForRow(at: indexPath)
            let section = indexPath.section
            let text = currentCell!.textLabel!.text!

            if currentCell?.accessoryType == .checkmark {

                switch section {

                case 0:
                    firstArray = firstArray.filter( { $0 != text } )
                    currentCell?.accessoryType = .none
                case 1:
                    secondArray = secondArray.filter( { $0 != text } )
                    currentCell?.accessoryType = .none

                case 2:
                    thirdString = ""
                    currentCell?.accessoryType = .none

                case 3:
                    fourthString = ""
                    currentCell?.accessoryType = .none

                default:
                    break

                }

            } else {

                switch section {

                case 0:
                    firstArray.append(text)
                    currentCell?.accessoryType = .checkmark
                case 1:
                    secondArray.append(text)
                    currentCell?.accessoryType = .checkmark

                case 2:
                    thirdString = text
                    currentCell?.accessoryType = .checkmark
                case 3:
                    thirdString = text
                    currentCell?.accessoryType = .checkmark

                default:
                    break

                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

更好的方法是将checkMark设置逻辑放在cellForRow内,因为可能会检查出非想要的单元格,因此在didSelectRow存储要检查的一个/ s的索引路径/ s使用选定的IndexPath

重新加载单元格