Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a row of UITableView to deSelect?

When i select a row of UITableView it becomes green(customized), but when i select another row, previously selected row do not stay green. I'm reloading my table after every row selection.I've tried this

NSIndexPath *selection = [mainTable indexPathForSelectedRow];
[mainTable selectRowAtIndexPath:selection animated:NO
scrollPosition:UITableViewScrollPositionNone]; 

but with this code i get only most recent selected row green(selected) I've searched for this and found some suggestions, most of them suggest to store index value of selected row in an array and use this array later, i've tried this but it did not work. Any other suggestion or sample code will be appreciated.

like image 703
Blios Avatar asked Nov 16 '12 06:11

Blios


2 Answers

Update:

A better way to achieve this would be to return nil from the UITableViewDelegate method tableView:willDeselectRowAtIndexPath: for the indexPath that you wish to stay selected.

Don't use this :

Try this add UITableViewDelegate method:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
  //use this for row u want to prevent to deSelect
  [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];               
}
like image 112
Paresh Navadiya Avatar answered Sep 29 '22 18:09

Paresh Navadiya


Do this:

mainTable.allowsMultipleSelection = YES;

From the UITableView Class Reference:

allowsMultipleSelection

A Boolean value that determines whether users can select more than one row outside of editing mode.

@property(nonatomic) BOOL allowsMultipleSelection

Discussion

This property controls whether multiple rows can be selected simultaneously outside of editing mode. 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. If you call indexPathsForSelectedRows, you can get the index paths that identify the selected rows.

The default value of this property is NO.

like image 45
crawler Avatar answered Sep 29 '22 19:09

crawler