سؤال

In my iOS app I have come across a problem with being able to select a table cell multiple times before some action is completed.

My situation is that I have the user type a value in and then hit submit. While trying to fully test my code I have noticed that submit can be pressed multiple times before my activity indicator shows. This results in multiple submissions.

How would I fix this for a table? Any suggestions would be great. Thanks.

هل كانت مفيدة؟

المحلول

In tableView:didSelectRowAtIndexPath: you could disable user interaction for the entire view (including all UITableViewCells):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view setUserInteractionEnabled:NO];
}

Then, wherever you're handling your operation's completion, re-enable the view:

-(void)yourOperationIsFinished{
    [self.view setUserInteractionEnabled:YES];
}

نصائح أخرى

You could use a flag that indicates if the cell has been selected at least once, which is initially NO. As soon as a table view cell is selected, the method tableView:didSelectRowAtIndexPath: is called. In this method, you could set a flag to YES.
If you implement also the method tableView:willSelectRowAtIndexPath:, which is called before tableView:didSelectRowAtIndexPath:, you could return either the submitted indexPath, as long as your flag is NO, or nil after the first call to tableView:didSelectRowAtIndexPath:. In this case, the cell will no longer be selected.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top