ios - 表格 View 中重复复选标记

标签 ios swift tableview sections checkmark

我在我的应用程序中实现了一个带有部分和复选标记的 tableView。 当我点击某个单元格时,我遇到了一个问题,复选标记出现在该单元格上,但在 12 行后重复。

我认为问题出在我的部分,“didSelectRowAtIndexPath”函数使用“indexPath.row”来标识单元格,但就像我有一些部分一样,我还需要指定“IndexPath.section”来确定哪个单元格哪个部分被点击。

这是我的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
    cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]

return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //Je compte le nombre de ligne dans tableArray et créer autant de cellule que de ligne
    return objectsArray[section].sectionObjects.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return objectsArray.count
}

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

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    //On affiche le boutton pour sauvegarder les catégories
    SaveCategorie.hidden = false

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        //Si la cellule est déja cochée
        if cell.accessoryType == .Checkmark
        {
            //je la décoche
            cell.accessoryType = .None 
        }
            else {
            cell.accessoryType = .Checkmark
            }
    }
}

尝试存储该项目:

var selectedRowNumber: NSMutableIndexSet!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
        cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]

        cell.accessoryType = .None
        if let selectedRowNumber = self.selectedRowNumber {
            if indexPath.row == selectedRowNumber {
                cell.accessoryType = .Checkmark
            }
        }
        return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        //On affiche le boutton pour sauvegarder les catégories
        SaveCategorie.hidden = false

        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            //Si la cellule est déja cochée


            cell.accessoryType = .Checkmark
            self.selectedRowNumber.addIndex(indexPath.row)

            dump(CatChoosen)
            dump(selectedRowNumber)
        }
    }

但我明白:

fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

TableViewCell 被重复使用,这就是为什么您会在第 12 行再次看到它,同一单元格已被重复使用。

保存数据中每个项目的复选标记。然后,当加载单元格时,检查是否设置了该标志,如果是,则设置复选标记。如果不是,则保留被清除。

关于ios - 表格 View 中重复复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837483/

相关文章:

ios - UINavigationController 在 swift 3 中为 nil

listview - 在 Tableview xamarin.forms 中创建 Listview

ios - System.setClipboard不适用于iOS(AS3)

swift - 知道为什么这个函数正在写入/更新表中的第一个单元格而不是 CFQuery TableViewController 中选定的单元格吗?

ios - Cocos2d v3 : How do you draw an arc?

Swift 3 - 接住并抛出

JavaFX:禁用 TableView 多列排序

swift - 如何将两个 View Controller 链接到以编程方式创建的 TableView ?

iphone - 应用不面向

ios - 使用 Flickr API 时的 Alamofire AFError