【问题标题】:Repeated checkmark in tableview表格视图中的重复复选标记
【发布时间】:2016-01-17 10:53:28
【问题描述】:

我已经在我的应用程序中实现了一个带有部分和复选标记的 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)
        }
    }

但我明白了:

致命错误:在展开可选值时意外发现 nil

【问题讨论】:

    标签: ios swift tableview sections checkmark


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2011-05-21
      • 2016-04-06
      • 2012-05-19
      相关资源
      最近更新 更多