Reputation: 529
I have one table view with showing button, title. now i need to check / uncheck the cell with image on cell.And at last what are the checked image cell title i need to print.How to do that?
my code:
var selectedIndexPathArray = Array<NSIndexPath>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BlockedTableViewCell
let pending = allnames?[indexPath.row]
cell.NameLabel.text = pending?.name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndexPathArray.append(indexPath as NSIndexPath)
tableView.reloadData()
}
My cell check box image name : 'checked.png' 'unchecked.png'
Upvotes: 1
Views: 1295
Reputation: 4375
Update Image :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BlockedTableViewCell
let pending = allnames?[indexPath.row]
cell.NameLabel.text = pending?.name
if selectedIndexPathArray.contains(indexPath) {
cell.checkboxBtn.setImage( UIImage(named:"checked.png"), for: .normal)
} else {
cell.checkboxBtn.setImage( UIImage(named:"unchecked.png"), for: .normal)
}
return cell
}
Printing Title :
func printSelectedItems(){
for indexPath in selectedIndexPathArray {
let item = allnames?[indexPath.row]
print(item.name)
}
}
Upvotes: 1