1
votes

At various points during my app's execution it is required to programatically select a row of a UITableView using

[self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle];

This works fine and the row is highlighted and marked as selected in the backend. The table is configured to allow multiple selections.

However, if the user subsequently deselects this row the "didSelectRowAtIndexPath" function is not being called. The strange thing is, if they reselect it, the function is called again as would be expected.

Any idea what could be going wrong? Is there an additional property or something that should be reset when selecting the row?

4

4 Answers

1
votes

If the user subsequently deselects the row, the associated UITableViewDelegate method will be called:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
3
votes

Implement UITableViewDelegate protocol .h:

@interface YourView : UIView <UITableViewDataSource,UITableViewDelegate>

then in .m:

self.tableView.delegate = self;

then write:

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

and your method will be call

1
votes

There are two methods for selecting/deselecting rows in UITableViewDelegate :

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

`- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath`
0
votes

Generally speaking, you should not expect delegate methods to be called if you triggered the action from code. They will only be triggered by user interaction. If you programmatically set the cell to selected then it is only natural that the user interaction to deselect will trigger the associated delegate method.