Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift Tableview复选标记错误的位置_Swift_Uitableview_Checkmark - Fatal编程技术网

Swift Tableview复选标记错误的位置

Swift Tableview复选标记错误的位置,swift,uitableview,checkmark,Swift,Uitableview,Checkmark,我试图在单元格上添加复选标记,但如果我点击第0节第0行, 第2节第0行也已选中。此外,如果点击第0节第1行,第3节第0行选中(如果我点击第2节第0行, 第0节第0行也已选中(如果点击第3节第0行,第0节第1行已选中) 如果快速滚动,所有检查都会消失。 我怎样才能解决这个问题?提前谢谢你 import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

我试图在单元格上添加复选标记,但如果我点击第0节第0行, 第2节第0行也已选中。此外,如果点击第0节第1行,第3节第0行选中(如果我点击第2节第0行, 第0节第0行也已选中(如果点击第3节第0行,第0节第1行已选中)

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

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

                }
            }
        }
    }

更好的方法是将复选标记设置逻辑放在
cellForRow
中,因为不需要的单元格可能会被检查,因此在
didSelectRow
中存储Indexpath/s/s,您需要检查并使用所选的
Indexpath
重新加载单元格。谢谢您的评论。但是如何定义
文本
?应用程序将崩溃,因为在展开可选值时,
意外发现nil
文本来自数组直接比较它不要在
didSelectRow
中重新加载整个表视图。只需重新加载一行。问题。在类定义中,您将该类声明为UITableViewDataSource和委托。然后在viewDidLoad()中执行相同的操作。为什么要做两次?此外,如果您使用了Interface Builder,您还必须通过Interface Builder进行操作。@MartinMuldoon您的评论应该是关于这个问题的,而不是这个答案。