Question

I have a table based app that stores data on a mysql server, it gets updated and writes the data to nsdictionary for persistance. When you make a change to the data, the table need to update. However if I put a [self tableview reloadData] the app crashes when selecting a row. Does anyone have an Idea on how to make the table refresh happen. Do I need to ditch this and make an object from the data and use that?

Thanks.

  -(void) loadData3;{

    MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:@"Rows"];
NSLog(@"AppDelegate.data3 : %@",AppDelegate.data3 );
NSLog(@"self.tableDataSource3 : %@",self.tableDataSource3 );

}

- (void)viewDidLoad {
    [super viewDidLoad]; 
    [self loadData3];
    if(CurrentLevel3 == 0) {
    self.navigationItem.title = @"Family";
}
else 
    self.navigationItem.title = CurrentTitle3;  
}
}


- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

 }

Here is the cellForRowAtIndexPath method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    NSDictionary *dictionary = [self.tableDataSource3 objectAtIndex:indexPath.row];
    cell.textLabel.text = [dictionary objectForKey:@"Title"];

    return cell;
}
Was it helpful?

Solution

I concur with the explanation that reloadData is the correct way to go, and you need to find and fix the bug that is causing your crash.

Here are two giant things to look at:

  1. Make sure immediately after altering the database, the call to numberOfRowsInSection will return the valid, new value as a result of the table being modified. (I have seen many people post issues due to this).

  2. Look carefully at your cellForRowAtIndexPath method. Remember, this method doesn't only set the data in the cells, but is also responsible for recycling cells. I have seen many instances where this was mishandled - and when table data is subsequently reloaded - and the cells need to be reclaimed and reassigned - things go haywire. I'd bet 99% that your issue is in here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top