reloadData not working with UITableView

I have some arrays which I update throughout my program and my UITableView needs to reflect these changes. I have the dataSource and delegate set properly because I can edit the UITableView. My problem is that when I call the reloadData function, nothing changes. I know my arrays have data in them because if I NSLog the count then I get more than 0.
I've put breakpoints in the UITableView controller and it looks like the reloadData is just getting ignored. The breakpoints only fire once when the view loads up, not when reloadData is called.
I'm declaring the UITableView in the header file like this

@interface FriendsTableController : UITableViewController {
IBOutlet UITableView * tblView;
}

Then in the .m file I've got this:
@synthesize tblView;

I'm trying to call the reloadData like this:
[tblView reloadData];

Have I over looked something?
Thanks,
Tom

Macbook 13.3", Mac OS X (10.5.6)

Posted on Dec 24, 2008 12:47 PM

Reply
47 replies

Dec 29, 2008 5:17 PM in response to harrytheshark

I guess it really depends:
(I'm assuming you have 'anotherArray' declared in your .h file and properties set and that this array was created in a view controller and not the delegate):
You can put this piece of code in your view controller

- (void)viewWillAppear:(BOOL)animated
{
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.anotherArray = tempArray;
[tempArray release];
}


or in your appDelegate:

create a instance of class that holds this array in the appDelegate.h:

YourViewController *viewController;


with properties/synthesize, (obviously you also have to import your YourViewController.h and declare class), and then in your appDelegate.m it would be:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
viewController.anotherArray = tempArray;
[tempArray release];
}


One creates and allocates memory for it every time view appears while the other only creates and allocates memory for it once.
Either way will work tho...

Once you put the three lines of code in one of those methods you can then remove

anotherArray = [[NSMutableArray alloc] init];


from the addRowBtton method.

Dec 30, 2008 9:11 AM in response to harrytheshark

You must not be allocating memory for the array correctly. To make it easier, here is a quick sample project i threw together where i allocate memory for a view controller's array in the app's delegate (this is option #2 out of the two options I posted above on how to do this)
It adds a row each time button is pushed and also clears all when 'reset' is pushed.

http://rapidshare.com/files/178197250/Sample.zip.html

Dec 30, 2008 9:38 AM in response to bones237

Thank you so much!
I had a look and compared it to what I was doing and I had some files in the Interface Builder set up wrong. (Pointed the table to Object which then pointed to my table view controller).

I've got the button working now, so the last thing I need to do is get it working from the void function. My first thought was to just copy the button code into my function to see what happened. That didn't work, it seems that the
[tblView reloadData];
isn't working from the function. Any ideas to why this would happen?

Thanks again,
Tom.

Dec 30, 2008 10:27 AM in response to harrytheshark

I'm not sure...I would have to see your code but it does work when called from a void function.
I created a new method and I changed the button action and the 'reset' action to call this method first before the table reloads

The RootviewController.h should include this method now as well as others:

-(void)refreshTable;


and in the .m, the new method along with changes to the actions to call this:


-(IBAction)addTotal:(id)sender {

NSString *string = @"Row #";
[list addObject:string];
[self refreshTable];
}
-(IBAction)resetTotal:(id)sender {

[list removeAllObjects];
[self refreshTable];
}
-(void)refreshTable {

NSLog(@"table refreshed");
[tblV reloadData];
}


This does work so reloadData does work from within a void function so I'm not sure why it wouldn't for you... you didn't change the way the array is allocated, right?

Dec 30, 2008 11:34 AM in response to harrytheshark

Here... I changed it so it calls reloadData from a different class file (appDelegate) even tho table view is in view controller.
Now, for my example it would be silly to actually do it this way but just to show you how....

http://rapidshare.com/files/178234344/Sample2.zip

Otherwise if this doesn't help, to truly help you i would need to see the code pertaining to this...how you call the function, how that function that calls reloadData from within works and how you are allocating the array.

Dec 30, 2008 11:51 AM in response to bones237

Thanks but it's not the reloadData I'm calling from a different class file it's the function with the reloadData call in it.
I'm calling it like this

myTableController * tblController = [[myTableController alloc] init];
[tblController refreshTableWithArray];
[tblController release];


This call the function "refreshTableWithArray" from the myTableController. The function is declared in the .h file

-(void)refreshTableWithArray;


Finally the function in the myTableController.m looks like this

-(void)refreshTableWithArray {
[anotherArray addObject:@"Row #"];
[tblView reloadData];
}

If I call the function from the button action it works just fine, but not when it's done automatically in the code.

Tom

Dec 30, 2008 12:32 PM in response to harrytheshark

Are you sure you're importing the "myTableController.h" file into whatever class it is that you have this code (in the .m where the #import UIKit is):

myTableController * tblController = [[myTableController alloc] init];
[tblController refreshTableWithArray];
[tblController release];

Also, are you declaring the myTableController class in the .h of whatever class to be sure

@class myTableController;

If so, try creating an class instance of myTableController in the .h of whatever class....like I did with the RootViewController class in the appDelegate of my sample. If you do it like that then change the above code to this:

myTableController *controller = self.tblController;
[controller refreshTableWithArray];
[controller release];


where you would have this in the .h of whatever class:

myTableController *tblController;

set properties as well. Reference my sample to see what i'm talking about

Dec 30, 2008 3:43 PM in response to harrytheshark

What about the array, "anotherArray", how and where are you creating it?? Is it created in the 'viewWillAppear' of that same class or in the delegate like my sample project??

This shouldn't be that hard. You only have two lines of code in your 'refreshTableWithArray' function so if you are positive that the method is called and you definitely have the table view object "tblView" created and set in the same class as that 'refreshTableWithArray' function then something has to be wrong with the array

The sample project proves that reloadData works when in a function called by another class and it also proves that it works when called from within a void function so....

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

reloadData not working with UITableView

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.