views:

57

answers:

1

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;
}
A: 

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.

Brad
Thanks, I'll look into that, new ground for me, but i'll start there.
Michael Robinson
Here's a boilerplate example. Notice the "dequeueReusuableCell" jazz. (Sorry about the formatting...) UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellIdent"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Set the label for the cell... [[cell textLabel] setText:[myArray objectAtIndex:[indexPath indexAtPosition:1]]; return cell;
Brad
@ Brad That did some wacky stuff, indexAtPostion i've never seen.
Michael Robinson
That line was just assuming that you had an NSString array "myArray" where all the cell labels were coming from. [indexPath indexAtPosition:1] would be the cell index within the first section - assuming your tableView only used a single section (i.e. [indexPath indexAtPosition:0] would return the section number - which would always be zero for a single section view)
Brad
@Brad Thanks, That wasn't the case but I will be using that format from something else soon.
Michael Robinson
Thanks.. I'm trying the NSURLconnection process within this UITableView Controller instead
Michael Robinson