Reputation: 127
I have a UITableView
which i want to add checkmarks to selected cells or cells where cell.textLabel.text
is contained in an array. If the array count is 0 the first cell should have a check mark (works but other cells are selected randomly) else add a check mark to cells where textlabel.text
is contained array, also works but upon scroll random cells are selected. (I've added multiple selection to my UITableView). How can i fix this issue?
cellForRowAtIndexPath
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = settings_categories[indexPath.row]
if (userData[0].settings_categories.count == 0 && indexPath.row == 0){
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else{
if (userData[0].settings_categories.contains(settings_categories[indexPath.row])){
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
}
if (cell.selected){
cell.selected = false
if (cell.accessoryType == UITableViewCellAccessoryType.None){
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else
{
cell.accessoryType = UITableViewCellAccessoryType.None
}
}
return cell;
}
didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
NSLog("You selected cell number: \(indexPath.row)!")
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell!.selected
{
cell!.selected = false
if cell!.accessoryType == UITableViewCellAccessoryType.None
{
//Add to array
userData[0].settings_categories.append((cell?.textLabel?.text)!)
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
print(userData[0].settings_categories)
}
else
{
var counter = 0;
for index in userData[0].settings_categories {
if index == cell?.textLabel?.text {
userData[0].settings_categories.removeAtIndex(counter)
print(userData[0].settings_categories)
}
counter++
}
cell!.accessoryType = UITableViewCellAccessoryType.None
}
}
}
Upvotes: 1
Views: 793
Reputation: 140
Set the accesory in the cell class implementation
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
self.accessoryType = selected ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
}
Upvotes: 0
Reputation: 4825
You can't store selected state inside the cell itself, since they are reused. You have to maintain separate array or indexset to maintain the selected indexes.
Upvotes: 1
Reputation: 1739
just add one more thing cell.accessoryType = UITableViewCellAccessoryType.None
after cell initialised because in tableview its reuse privious cell so if cell has already checked(because it reuse the checked cell) then it by default come as checked so after update your code will be
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = settings_categories[indexPath.row]
cell.accessoryType = UITableViewCellAccessoryType.None// new added line
if (userData[0].settings_categories.count == 0 && indexPath.row == 0){
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
// cell.selected = true
}
else{
if (userData[0].settings_categories.contains(settings_categories[indexPath.row])){
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
// cell.selected = true
}
}
if (cell.selected){
cell.selected = false
if (cell.accessoryType == UITableViewCellAccessoryType.None){
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else
{
cell.accessoryType = UITableViewCellAccessoryType.None
}
}
return cell;
}
Upvotes: 3
Reputation: 1713
Happened with me too. I, then, maintained an array to save selected indexPath.row & reloaded Table Data with that array such as rows which are saved in that array will have Checkmark Accessory after every scroll. This is just a work around and not the best fix.
Upvotes: 0