Blios Blios - 3 months ago 10
iOS Question

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.

Answer

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.