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.
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.
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];
}
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With