滚动UITableView时,复选标记消失

时间:2016-05-04 19:12:39

标签: ios swift uitableview

正如标题所说,我的复选标记正在消失。我发现的答案只是针对特定代码进行了修复,所以我不确定我的错误。

任何帮助都将不胜感激。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cellx: UITableViewCell = self.tableViewOut.dequeueReusableCellWithIdentifier("cellx", forIndexPath: indexPath)



    // Configure the cell...

    switch (indexPath.section) {


    case 0:
        cellx.textLabel?.text = "\(qUnitArray[indexPath.row])\t\t\t\(functionList.searchUnitCount(ttopicArray, funitSearch: qUnitArray[indexPath.row])) \t \(functionList.searchUnitRating(ttopicArray, fDataStore: tDataStore, funitRating: qUnitArray[indexPath.row]))"
        cellx.accessoryType = .None
        conUnit = qUnitArray[indexPath.row]

        conUnitCount = 0
        while conUnitCount < selectedUnit.count {
            if selectedUnit[conUnitCount] == qUnitArray[indexPath.row] {
                cellx.accessoryType = .Checkmark
                print("checkmark change")
            }
            conUnitCount += 1 
        }
    case 1:
        cellx.textLabel?.text = "\(qsubjectArray[indexPath.row])\t\t\t\(functionList.searchSubjectCount(ttopicArray, fsubjectSearch: qsubjectArray[indexPath.row])) \t \(functionList.searchSubjectRating(ttopicArray, fDataStore: tDataStore, fsubjectRating: qsubjectArray[indexPath.row]))"
        cellx.accessoryType = .None
        conSubject = qsubjectArray[indexPath.row]

        conSubjectCount = 0
        while conSubjectCount < selectedSubject.count {
            if selectedSubject[conSubjectCount] == qsubjectArray[indexPath.row] {
                cellx.accessoryType = .Checkmark
                print("checkmark change")

            }
            conSubjectCount += 1
        }
    default:
        cellx.textLabel?.text = "Other"

    }






func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cellx = tableView.cellForRowAtIndexPath(indexPath) {
        if cellx.accessoryType == .Checkmark
        {
            cellx.accessoryType = .None

            if indexPath.section == 0 {
                while tempUnitCount < tselectedArray[0].count {
                    if qUnitArray[indexPath.row] == tselectedArray[0][tempUnitCount] {
                        tempUnitCode = tempUnitCount
                    }
                    tempUnitCount += 1 
                }
                tselectedArray[0].removeAtIndex(tempUnitCode)
                tempUnitCode = 0
                tempUnitCount = 0
            } else {
                while tempSubjectCount < tselectedArray[1].count {
                    if qsubjectArray[indexPath.row] == tselectedArray[1][tempSubjectCount] {
                        tempSubjectCode = tempSubjectCount
                    }
                    tempSubjectCount += 1 
                }

                tselectedArray[1].removeAtIndex(tempSubjectCode)
                tempSubjectCode = 0
                tempSubjectCount = 0
            }
        }
        else {
            cellx.accessoryType = .Checkmark

            if indexPath.section == 0 {
                if tselectedArray[0] == [""] {
                    tselectedArray[0][0] = qUnitArray[indexPath.row]
                } else {
                    tselectedArray[0].append(qUnitArray[indexPath.row])
                }
            }else {
                if tselectedArray[1] == [""] {
                    tselectedArray[1][0] = qsubjectArray[indexPath.row]
                } else {
                    tselectedArray[1].append(qsubjectArray[indexPath.row])
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

不要在此方法中设置accessoryType。不要在这里以任何方式改变细胞。

cellForRowAtIndexPath:按需获取单元格,并重复使用相同类型的单元格。因此,更改数据源中的某些内容,以便它知道新状态并调用其中一个tableView重新加载函数,以使其重新生成单元格。

当您的单元格滚动然后重新打开时,它会调用cellForRowAtIndexPath: - 您正在更改的单元格不是对表格的永久更改。