滚动时UITableView Checkmarks消失

时间:2015-01-13 09:19:08

标签: ios swift tableview checkmark

我必须在tableView上制作复选标记,但如果我滚动并且一个检查标记的单元格不可见,我向后滚动复选标记消失。

运行此代码时

var boolArray = [Bool]()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)      {




        var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!


        if cell.accessoryType == UITableViewCellAccessoryType.Checkmark {

            cell.accessoryType = UITableViewCellAccessoryType.None

            boolArray[indexPath.row] = false


        }
        else
        {

            cell.accessoryType = UITableViewCellAccessoryType.Checkmark

            boolArray[indexPath.row] = true

        }

    println(boolArray)


}
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->    UITableViewCell
{
    boolArray.append(false)
        var view = UITableViewCell(style: UITableViewCellStyle.Default,    reuseIdentifier: "CellTable")


        return view

}

经过一点点滚动和勾选后,印刷的阵列就是这么大......

[true,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]

5 个答案:

答案 0 :(得分:4)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->    UITableViewCell{
var cell : UITableViewCell = .........
if(boolArray[indexPath.row){
    cell.accessoryType = UITableViewCellAccessoryType.Checkmark
} else {
    cell.accessoryType = UITableViewCellAccessoryType.None
}
}

试试这段代码。

答案 1 :(得分:4)

创建一个元组数组来存储行和节值:

var selectedCells:[(row: Int, section: Int)] = []

cellForRowAtIndexPath中,检查您是否有selectedCellValues。如果是这样,请为该单元格添加accessoryType并打开循环,

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

    let cell = tableView.dequeueReusableCellWithIdentifier("SettingsCell", forIndexPath: indexPath) as SettingsCustomCell

    var rowNums:NSNumber = indexPath.row
    var sectionNums:NSNumber = indexPath.section

    var selectedRow:Int
    var selectedSection:Int

    if(selectedCells.count > 0){
        for tuple in selectedCells{

            selectedRow = tuple.row
            selectedSection = tuple.section

            if(selectedRow == rowNums && selectedSection == sectionNums){
                cell.accessoryType = UITableViewCellAccessoryType.Checkmark
                break
            }else{
                cell.accessoryType = UITableViewCellAccessoryType.None
            }
        }
    }else{
        cell.accessoryType = UITableViewCellAccessoryType.None
    }

    var keyValue = listOfSectionTitles[indexPath.section]
    var items: [NSString] = dictionaryOfCellTitles.objectForKey(keyValue) as [NSString]

    cell.textLabel?.text = items[indexPath.row]
    return cell
}

处理selectedcellvalues中的didSelecteRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.cellForRowAtIndexPath(indexPath)

    if(cell?.accessoryType == UITableViewCellAccessoryType.None){
        cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
        selectedCells.append(row:indexPath.row, section:indexPath.section)
    }else{
        var rowToDelete = indexPath.row
        var sectionToDelete = indexPath.section

        for var i=0; i < selectedCells.count; i++
        {
            if(rowToDelete == selectedCells[i].row && sectionToDelete == selectedCells[i].section){
                selectedCells.removeAtIndex(i)
                cell?.accessoryType = UITableViewCellAccessoryType.None
            }
        }
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

答案 2 :(得分:1)

NSMutableIndexSet是用于跟踪选择状态的更好对象。

返回indexPath的单元格时,还需要检查选择状态

var selectedCells = NSMutableIndexSet()

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

    var accessory=UITableViewCellAccessoryType.none

    if (selectedCells.containsIndex(indexPath.row) {
       selectedCells.removeIndex(indexPath.row)
    }
    else {
       selectedCells.addIndex(indexPath.row)
       accessory=.checkmark
    }

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {

        cell.accessoryType = accessory
    }

}

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) ->    UITableViewCell
{
    var cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"CellTable")

     var accessory=UITableViewCellAccessoryType.none

     if (selectedCells.containsIndex(indexPath.row) {
         accessory = .checkmark
     }

     view.accessoryType=accessory

        return view
}

答案 3 :(得分:0)

执行cellForRowAtIndexPath时,您的错误位于boolArray.append(false)。每次绘制单元格时都会调用cellForrRowAtIndexPath: ,即使它已在之前绘制。如果indexPath.row大于boolArray的长度,则可能的修复只是附加false,否则它是您的模型&#39;你只需检查它。

答案 4 :(得分:0)

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->    UITableViewCell
从代码中可以看到

重用单元格。您需要保持所选单元格的状态,并在var view = UITableViewCell ...

之后指定附件视图状态