iosobjective-cuitableviewsetneedsdisplay

Tableview reload with setNeedsDisplay not working


I have a tableview in a view that lists all the steps that will be executed. I want to show a step as marked when the step was executed. The problem I am facing is that the tableview does not refresh until the procedure executed all the steps. Consequently all the steps show as marked at the same time.

This is my function which starts running when the user presses a button:

-(void)Initialiseer {
    //do something for the first step
    [self.MyTableView reloadData];
    [self.MyView setNeedsDisplay];

    //do something for the second step
    [self.MyTableView reloadData];
    [self.MyView setNeedsDisplay];

    //do something for the third step
    [self.MyTableView reloadData];
    [self.MyView setNeedsDisplay];
}

MyView is the view IBOutlet. MyTableView is the tableview IBOutlet. I also tried [self.view setNeedsDisplay] and [self. MyTableView setNeedsDisplay] but that does not work either. I also tried getting the cell from the tableview and making the changes on the cell itself. Nothing is refreshed until the procedure finished executing completely...

Can someone tell me what I am doing wrong? I am new to iOS development and I searched, read, tried... but did not find an answer so far.


Solution

  • I think you need to read up on the run loop and setNeedsDisplay (which I don't think does what you're expecting). Basically, the UI does not get updated until your code finishes executing. The pattern you need for displaying a complex calculation is something like this:

    -(void)Initialiseer {
      dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
        //do something for the first step
    
        dispatch_sync(dispatch_get_main_queue(), ^{
          [self.MyTableView reloadData];
        });
    
        //do something for the second step
        dispatch_sync(dispatch_get_main_queue(), ^{
          [self.MyTableView reloadData];
        });
    
        //do something for the third step
        dispatch_sync(dispatch_get_main_queue(), ^{
          [self.MyTableView reloadData];
        });
      });
    }