问题
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
}
回答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
}
回答2:
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
}
}
来源:https://stackoverflow.com/questions/40255463/multi-select-is-not-working-properly-in-ios