0

The app I'm creating launches with a table view. I can immediately add a new information, save the information to a plist, and reload the view. The new cell and its data shows up at the bottom. When I re-run the app however, it re-orders the order I originally saw them.

The titles of the cell are stored in a variable called countdowns and the descriptions are stored in countdown_info. I then use these variables and store them as a dictionary in the plist.

Here's an example that has the same result every time. The following is the table view as it would appear if I had just added 4 new cells:

**[CODE 1]**
Countdown 1
  Description 1
Countdown 2
  Description 2
Countdown 3
  Description 3
Countdown 4
  Description 4

However, when the app re-runs/re-builds, the app opens to this:

**[View After Re-Run]**
Countdown 4
  Description 4
Countdown 1
  Description 1
Countdown 3
  Description 3
Countdown 2
  Description 2

Some NSLogs I've added reveal the table from the file are saved as [Code 1], and that indexPath.row on the table view increase from 0-3, but for some reason, the view reorganizes with a different pattern.

What is the cause of this? Thanks!

Code from cellRowAtIndexPath

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"] autorelease];
}

ClockworkAppDelegate *dataCenter = (ClockworkAppDelegate *)[[UIApplication sharedApplication] delegate];

cell.textLabel.text = [[dataCenter countdowns] objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [[dataCenter countdown_info] objectAtIndex:indexPath.row];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}
drfranks3
  • 667
  • 8
  • 21

1 Answers1

2

NSDictionaries are unordered, so you are probably losing the ordering when saving to NSUserDefaults. You'll have to store the order somehow as well. See this thread for some ideas.

Community
  • 1
  • 1
pepsi
  • 6,785
  • 6
  • 42
  • 74
  • The thread you linked to gave me some great tips! It seems I just needed an Enumerator to help re-organize the data. Thanks! – drfranks3 Jun 12 '11 at 19:13