0

I have a problem with setting the checkmark for a row in iOS table view If I select one element above, the next 13th element is also getting selected, I not sure why?

Should I have to do something with the table before setting the checkmark, cause I am just checking one condition and if that condition is true I am setting the accessoryType as checkmark, below is the code.

Note:- When this happen the 13th row will not get selected, it just changes the accessory type of that row.

if let cell = tableView.cellForRowAtIndexPath(indexPath) {
                if cell.selected {
                    if(self.sections[indexPath.section].files[indexPath.row].type != "cloud"){
                        print(self.sections[indexPath.section].files[indexPath.row])
                        cell.accessoryType = .Checkmark
                        NSNotificationCenter.defaultCenter().postNotificationName("enableOptions", object: nil)
                    }
                }
            }

CellForIndexPath Code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyFilesTableViewCell

        let fileSection = sections[indexPath.section]
        let file = fileSection.files[indexPath.row]

        cell.title.text = file.name
        if file.timeStamp.isEmpty{
            cell.timeStamp.hidden = true
        }else{
            cell.timeStamp.hidden = false
            cell.timeStamp.text = file.timeStamp
        }
        cell.icon.image = file.icon
        cell.actionsBtn.row = indexPath.row
        cell.actionsBtn.section = indexPath.section
        cell.actionsBtn.setTitle("\u{f142}", forState: .Normal)
        cell.actionsBtn.addTarget(self, action: #selector(MyFilesTableViewController.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        if(editingTable){
            cell.actionsBtn.hidden = true
        }else{
            cell.actionsBtn.hidden = false
        }
        if(file.type == "cloud"){
            cell.actionsBtn.hidden = true
        }
        cell.progressBar.progress = 0.0
        cell.progressBar.hidden = true
        return cell
    }
Prashanth Kumar B
  • 566
  • 10
  • 25

2 Answers2

1

Its problem with your cell reusable in cellForRowAtIndexPath. Kindly use the below code.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyFilesTableViewCell

cell.accessoryType = .None

if cell.selected {
                if(self.sections[indexPath.section].files[indexPath.row].type != "cloud"){
                    print(self.sections[indexPath.section].files[indexPath.row])
                    cell.accessoryType = .Checkmark
                }
            }

let fileSection = sections[indexPath.section]
let file = fileSection.files[indexPath.row]

cell.title.text = file.name
if file.timeStamp.isEmpty{
    cell.timeStamp.hidden = true
}else{
    cell.timeStamp.hidden = false
    cell.timeStamp.text = file.timeStamp
}
cell.icon.image = file.icon
cell.actionsBtn.row = indexPath.row
cell.actionsBtn.section = indexPath.section
cell.actionsBtn.setTitle("\u{f142}", forState: .Normal)
cell.actionsBtn.addTarget(self, action: #selector(MyFilesTableViewController.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
if(editingTable){
    cell.actionsBtn.hidden = true
}else{
    cell.actionsBtn.hidden = false
}
if(file.type == "cloud"){
    cell.actionsBtn.hidden = true
}
cell.progressBar.progress = 0.0
cell.progressBar.hidden = true
return cell
}
Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38
  • @Aoxi - Thank you.. Its good for developers to keep in mind when working with reusability in `tableview` and `collectionview` in ios. Always assign the default values prior to the original values and so the cells will not collapse with the various contnet of the array or anyother collection. – Balaji Kondalrayal Oct 26 '16 at 07:08
0

By using this method you can omit your entire tableView reload and only modify your selected cell content.

create a array which store information of selected row of cell

myArray:[Int] = []

In your cellForRowAtIndexPath method set

cell.actionsBtn.tag = indexPath.row 
myArray.insert(1, at: indexPath.row)

instead of

cell.actionsBtn.row = indexPath.row`

In your buttonClicked(_:) method use as

 func buttonClicked( sender: AnyObject)
{

    let indexPath = IndexPath(row: sender.tag, section: 0)
    let cell = tableView.cellForRow(at: indexPath) as! MyFilesTableViewCell
    if myArray[sender.tag] == 1
    {
        cell.actionsBtn.setImage(UIImage(named:"checkmark_box"), for: .normal)
        myArray[sender.tag] = 0
    }
    else
    {
        cell.actionsBtn.setImage(UIImage(named:"tick_mark"), for: .normal)
        myArray[sender.tag] = 1
    }
}
Rizwan Shaikh
  • 2,824
  • 2
  • 27
  • 49