jdog
jdog

Reputation: 10759

Highjack or prevent UITableView didSelectRowAtIndexPath

I am trying to find a way to prevent or highjack didSelectRowAtIndexPath:. When a user selects a row in the tableview I want first throw an alertview saying "Data will be removed from CD if you do this." "We will be syncing the data to the server should you continue".

The user clicks yes continue. I want to prevent the new cell from being selected until the syncing completes. Should the syncing fail I want to pop an alert telling the user it failed and then stop didSelectRowAtIndexPath: from firing, thus preventing the new cell they touched from being selected.

Should the sync be successful I then want didSelectRowAtIndexPath: to be called.

Would the best way be to highjack willSelectRowAtIndexPath:?

Upvotes: 1

Views: 1245

Answers (2)

jdog
jdog

Reputation: 10759

Got it.

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableView *roomTable = tableView;
Rooms *room = [roomArray objectAtIndex:indexPath.row];
NSString *message = [NSString stringWithFormat:@"Switching rooms will remove data for current thing. Need to download %@ room", room.name];
[UIActionSheet actionSheetWithTitle:message 
                            message:@"Message" 
             destructiveButtonTitle:@"Continue" 
                            buttons:[NSArray arrayWithObjects:nil]
                         showInView:self.view 
                          onDismiss:^(int buttonIndex)
 {
     NSLog(@"User selected to change the room");
     [roomTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
 }
                           onCancel:^
 {
     NSLog(@"Change Room Cancelled");
 }];

return nil;   
}

Upvotes: 0

Warren Burton
Warren Burton

Reputation: 17382

Implement the delegate method

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

and return nil for the relevant indexPath

This will stop the relevant cell being selected.

From the docs for UITableViewDelegate

Return Value An index-path object that confirms or alters the selected row. Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don't want the row selected.

Upvotes: 5

Related Questions