Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are checkmarks not being displayed when UITableView.allowsMultipleSelection is enabled?

I have a UITableView in an iOS5.1 app where I set

self.tableView.allowsMultipleSelection=YES;

The Apple documentation states "When the value of this property is YES, a check mark is placed next to each row that is tapped. Tapping the row again removes the check mark.".

I am able to select multiple rows as the background is set to Blue. However, no checkmarks are displayed. Does the checkmark need to be set as shown below in didSelectRowAtIndexPath because I am using custom UITableViewCells?

cell.accessoryType = UITableViewCellAccessoryCheckmark;
like image 949
ChrisP Avatar asked Mar 21 '12 19:03

ChrisP


2 Answers

I do the checkmarks manually in my uitableviewcell subclasses. You are going to have to do the UITableViewCellAccessoryCheckmark manually in didSelectRowAtIndexPath and keep a track of which one is selected. I would recommend something like so:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableview cellAtIndex:indexPath];
    if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
     cell.accessoryType = UITableViewCellAccessoryNone;
}

note: I did not test this, but should give you the basic idea. Let me know if you have any questions. Did you try using a default uitableviewcell and see if it did the checkmark? I would not think a subclass would have a problem, as long as you are not modifying in the subclass.

like image 187
daltoniam Avatar answered Nov 01 '22 20:11

daltoniam


Another option is to use tableView:didDeselectRowAtIndexPath: in addition to tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.accessoryType = UITableViewCellAccessoryCheckmark;

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.accessoryType = UITableViewCellAccessoryNone;

}

like image 39
jay492355 Avatar answered Nov 01 '22 22:11

jay492355