3

I have an ivar of UITableView* myTableView. I have data being loaded from internet so when the data gets loaded should i call [myTableView setNeedsDisplay], i know i can/should call [myTableView reloadData] but the confusion is that calling setNeedsDisplay should also work but is not working.

I am just looking for an explanation as to what setNeedsDisplay does? when called on UITableView instance.

As per the documentation setNeedsDisplay calls drawRect in "next drawing cycle". so can anyone tell me what is the next drawing cycle.

5 Answers 5

6

setNeedsDisplay method has nothing to do with the reloading of the tableView. It just redraws the view of the tableView, not its cells or separators.

setNeedsDisplay and reloadData are entirely for different purposes.

5

setNeedsDisplay marks the calling UIView as needing to be redrawn is a purely visual sense, which as stated will happen on the next drawing cycle.

This is completely different to reloading the data when used in a UITableView context.

See this article for a little more information on the subject.

1
  • 1
    Link doesn't go to relevant content
    – vharron
    Jan 8, 2019 at 3:49
2

SetNeedsDisplay

A good example of this is when a UIImageView is rendered with one image; and while it is still on screen, you change the image to something else. In this case, just changing the image will not always trigger a repaint of the view, for that to occur you need to make a call to setNeedsDisplay.

reloadData

It has to be called when the underlying data source for the tableview is changed and you want that to be refelected in UI.In that case call this reloadData mehod

1

Generally, you only need to call setNeedsDisplay on custom views for which you have implemented drawRect.

0

My solution was to update all visible cells and keep track of indexPath of cell in my custom class "ProductTableViewCell" with reloadRowsAtIndexPaths, see below:

NSArray *arrayCells = [self.tableView visibleCells];
NSMutableArray *mArrayIndexPaths = [[NSMutableArray alloc] init];
for(ProductTableViewCell *cell in arrayCells) {
    [mArrayIndexPaths addObject:cell.indexPath];
}

[self.tableView beginUpdates];
[self.tableView endUpdates];
[self.tableView reloadRowsAtIndexPaths:[mArrayIndexPaths copy] withRowAnimation:UITableViewRowAnimationAutomatic];

Here is how the class is defined:

@interface ProductTableViewCell : UITableViewCell {
    NSString *reuseID;
}

@property (nonatomic, strong) NSIndexPath *indexPath;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.