Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent clicking UITableView second time

My app calls a block in tableView:didSelectRowAtIndexPath and in the block it presents a view controller. If I click the cell second time when the first click is in progress, it crashes. How can I prevent the cell to be clicked second time?

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


    [dataController fetchAlbum:item
         success:^(Album *album) {
            ...
            ...

            [self presentViewController:photoViewController animated:YES completion:nil];


               }];
like image 673
zontragon Avatar asked Nov 30 '22 18:11

zontragon


2 Answers

At the beginning of didSelectRow, turn off user interaction on your table.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.userInteractionEnabled = NO;
    ...

You may want to turn it back on later in the completion of fetchAlbum (Do this on the main thread) so that if the user comes back to this view (or the fetch fails), they can interact with the table again.

like image 158
Stonz2 Avatar answered Dec 06 '22 23:12

Stonz2


For swift 3 :

When user select a row, turn off user interactions :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tableView.isUserInteractionEnabled = false

Don't forget to turn it on back whenever the view appear :

override func viewDidAppear(_ animated: Bool) {
    tableView.isUserInteractionEnabled = true
}
like image 33
Skaal Avatar answered Dec 07 '22 01:12

Skaal