Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's with [UITableView reloadData]?

Tags:

I have an application that has a UITableView. This UITableView is populated by an NSMutableArray being held (as a property) in the appDelegate. You can think of this as an email window. It lists messages in a subclassed UITableViewCell. When a new message appears, I have all the code done which downloads the message, adds the data to the appDelegate's NSMutableArray which holds all of the messages. This code is working fine.

Now, once the new message is downloaded and added to the array, I am trying to update my UITableView using the following code, however - the UITableView's delegate functions do not get called.

The odd thing is when I scroll my UITableView up and down, the delegate methods finally get called and my section headers DO change (they show the message count for that section). Shoudn't they update in real-time and not wait for my scrolling to trigger the refresh? Also, the new cell is never added in the section!!

Please Help!!

APPDELEGATE CODE:

[self refreshMessagesDisplay]; //This is a call placed in the msg download method  -(void)refreshMessagesDisplay{     [self performSelectorOnMainThread:@selector(performMessageDisplay) withObject:nil waitUntilDone:NO]; }  -(void)performMessageDisplay{     [myMessagesView refresh]; } 

UITableViewController Code:

-(void) refresh{     iPhone_PNPAppDelegate *mainDelegate = (iPhone_PNPAppDelegate *)[[UIApplication sharedApplication] delegate];      //self.messages is copied from appDelegate to get (old and) new messages.     self.messages=mainDelegate.messages;      //Some array manipulation takes place here.      [theTable reloadData];     [theTable setNeedsLayout];  //added out of desperation     [theTable setNeedsDisplay];  //added out of desperation } 
like image 724
Dutchie432 Avatar asked Apr 30 '09 15:04

Dutchie432


People also ask

What does Tableview reloadData do?

reloadData()Reloads the rows and sections of the table view.

What is UITableView?

UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.

What class does UITableView inherit from?

The appearance of the tableview is managed by UITableView class, which inherits UIScrollView. In tableview, the row is simulated by the object of the UITableViewCell class, which can be used to display the actual content. We can customize the tableview cells to display any content in the iOS application.

What is the function of the Tableview (_ Tableview UITableView Numberofrowsinsection section int method?

Tells the data source to return the number of rows in a given section of a table view.


2 Answers

As a sanity check, have you verified that theTable is not nil at that point?

like image 84
smorgan Avatar answered Sep 21 '22 04:09

smorgan


You could try putting a delay on the reloadData call - I had a similar problem when I was trying to get my tableview to update when reordering cells, except that the app crashed if I called reloadData during it.

So something like this might be worth a try:

Refresh method:

    - (void)refreshDisplay:(UITableView *)tableView {     [tableView reloadData];  } 

and then call it with (say) a 0.5 second delay:

[self performSelector:(@selector(refreshDisplay:)) withObject:(tableView) afterDelay:0.5]; 

Hope it works...

like image 36
h4xxr Avatar answered Sep 23 '22 04:09

h4xxr