views:

8634

answers:

3

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
}
+5  A: 

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

smorgan
theTable is not nil, but now that you mention it, it does show as address 0x0. The IBOutlet theTable is linked to the control in IB, and the table remains visible throught this entire process....
Dutchie432
0x0 actually is nil-- the table view exists of course, but your pointer to it hasn't been set correctly. Double check your nib.
Marc Charbonneau
That's definitely the problem then. For some reason (impossible to say without seeing all the code) your 'theTable' variable is nil, so the reloadData message isn't going anywhere.
smorgan
Dutchie432
Why not use self.tableView instead of maintaining a separate reference to the table?
dmercredi
Because I am not my own hero, like you are mine now. I have much to learn!! Thank you all!!
Dutchie432
A: 

Have you tried setting a breakpoint in your refresh method just to be sure your messages array has the correct content before calling reloadData?

Marc Charbonneau
Yes. That method has been reviewed many times. I can see my Array count increase, and can even see my new object (message) in the debugger with all of the appropriate values.
Dutchie432
+1  A: 

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...

h4xxr