cmm932
cmm932

Reputation: 1

UI table view cell check marks repeating swift

My when a table cell is checked and you scroll down a check mark is repeated. I know this is due to cell reuse, but don't know how to fix it.

function to populate table

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

    let item = self.myEvents[indexPath.row]

    let cell = tableView.dequeueReusableCellWithIdentifier("row", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel!.text = self.myEvents[indexPath.row]
    return cell
    }

//function to make the table checkable

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("indexpath: \(indexPath)")
    let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    let text = cell.textLabel!.text
    if cell.accessoryType == UITableViewCellAccessoryType.None {
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        //let text = cell.textLabel!.text
        if(checkedEvents[0] == ""){
            checkedEvents[0] = text!
        }else{
            checkedEvents.append(text!)
        }
    } else {
        cell.accessoryType = UITableViewCellAccessoryType.None
        var index = 0
        for event in checkedEvents{
            if event == text{
                self.checkedEvents.removeAtIndex(index)
                index++
            }
        }
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

}

Upvotes: 0

Views: 1468

Answers (2)

Daniel Richter
Daniel Richter

Reputation: 1

If you want only one selection, put tableView.reloadData() in your didSelectRowAtIndexPath function

Upvotes: 0

Dave Batton
Dave Batton

Reputation: 8845

First, you need to store the number of the selected row somewhere. How about self.selectedRowNumber?

var selectedRowNumber: Int? = nil

Set this when the user selects a row (short version):

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    cell.accessoryType = .Checkmark
    self.selectedRowNumber = indexPath.row

    // You'll also need some code here to loop through all the other visible cells and remove the checkmark from any cells that aren't this one.
}

Now modify your -tableView:cellForRowAtIndexPath: method to clear the accessory if it's not the selected row, or add it if it is:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("row", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel!.text = self.myEvents[indexPath.row]

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

This code was written here in the browser, and may need some fixes to compile.

Upvotes: 1

Related Questions