Aoxi Aoxi - 1 month ago 10
Swift Question

Multi select is not working properly in IOS

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
}

Answer

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
}