Bassman
Bassman

Reputation: 413

UITableView not refreshed

I have an app consisting of a TabBar with a few TabBarControllers. One Controller contains a very simple table, which is supposed to display the contents of a NSMutableDictionary. When you hit the appropriate button, the Dictionary is updated in a separate Controller and the view switches to the UITableViewController, displaying the newly updated table.

I can see the Dictionary being updated. But the TableView never reflects the changes. In fact, it seems to display the changes only the 1st time I enter that screen.

I have tried [self table.reloadData] and while it gets called, the changes aren't reflected to the UITableView.

Does anyone have any suggestions? I am happy to post code, but am unsure what to post.

Update: the table is updated and refreshed properly only the 1st time it is displayed. Subsequent displays simply show the original contents.

Background: The tableview gets filled from a dictionary: appDelegate.currentFave. The tableview should get refreshed each time the ViewController is invoked by the TabBarController.

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"in viewWillAppear");

    [super viewWillAppear:animated];

    [self loadFavesFile];
    [self.tableView reloadData];
}

// load the Favorites file from disk
- (void) loadFavesFile 
{   
// get location of file
NSString *path = [self getFavesFilePath];

// The Favorites .plist data is different from the Affirmations in that it will never be stored in the bundle. Instead,
// if it exists, then use it. If not, no problem.
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {

    // read Faves file and store it for later use...
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    appDelegate.sharedData.dictFaves = tempDict;    

    // grab the latest quote. Append it to the list of existing favorites
    NSString *key = [NSString stringWithFormat:@"%d", appDelegate.sharedData.dictFaves.count + 1];

    NSString *newFave = [NSString stringWithFormat:@"%@", appDelegate.currentFave];
    [appDelegate.sharedData.dictFaves setObject:newFave forKey:key];

} else {
    NSLog(@"Favorites file doesn't exist");
    appDelegate.sharedData.dictFaves = nil;     
}
}


// this gets invoked the very first call. Only once per running of the App.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// reuse or create the cell
static NSString *cellID = @"cellId";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID];  
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}

// allow longer lines to wrap
cell.textLabel.numberOfLines = 0; // Multiline
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.font = [UIFont fontWithName:@"Chalkduster" size:(16)];
cell.textLabel.textColor = [UIColor yellowColor];   

// NOTE: for reasons unknown, I cannot set either the cell- or table- background color. So it must be done using the Label.

// set the text for the cell
NSString *row = [NSString stringWithFormat:@"%d", indexPath.row + 1];
cell.textLabel.text = [appDelegate.sharedData.dictFaves objectForKey:row];
return cell;    
}

Upvotes: 2

Views: 1398

Answers (4)

Bassman
Bassman

Reputation: 413

I found the problem. I was not properly initializing and assignng the TableView in my view controller. See below

- (void)viewDidLoad
{
   [super viewDidLoad];

   tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]     style:UITableViewStylePlain];

   tableView.dataSource = self;
   tableView.delegate = self; 
   tableView.backgroundColor=[UIColor blackColor];
   self.view = tableView;
}

Upvotes: 1

eric.mitchell
eric.mitchell

Reputation: 8855

You should make sure that your Interface Builder connections are set up properly, but what this problem really sounds like is that you have your UITableViewCell setup code in cellForRowAtIndexPath: inside your if(cell == nil) statement. Which it shouldn't be. Let me explain. If you have a list of cells, and you want to set the titles to each cell to a string in an array called myArray, right now your (incorrect) code looks like this:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if (cell == nil) {
        // No cell to reuse => create a new one
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"] autorelease];

        [[cell textLabel] setText:[myArray objectAtIndex:[indexPath row]]];
    }
    return cell;
}

Can you see the problem with that logic? The cell will only get an updated title if no reusable cell can be found, which, in your case, sounds like the situation. Apple says that you should create a 'new' cell each time cellForRowAtIndexPath: is called, which means that you put all of your setup code outside of the if(cell == nil) check.

Continuing with this example, the proper code would look like this:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if (cell == nil) {
        // No cell to reuse => create a new one
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"] autorelease];
    }

    [[cell textLabel] setText:[myArray objectAtIndex:[indexPath row]]];
    return cell;
}

This way, the cell gets assigned the proper string whether or not a reusable cell is found and so calling reloadData will have the desired effect.

Upvotes: 0

danielbeard
danielbeard

Reputation: 9149

I had this same problem yesterday, for me it turned out I had set the wrong file owner in interface builder and hadn't set up the data source and delegates for the table view properly.

Try going into interface builder and right-clicking on the file owner, this should show you if anything isn't connected up properly.

Upvotes: 0

Jumhyn
Jumhyn

Reputation: 6777

Assuming the code you have put up is correct, you want to use [self.table reloadData]. You have the . in the wrong place.

Upvotes: 0

Related Questions