0

I have a simple tableview, which displays a list of historical input values. This list is saved in a plist, and loaded within the "ViewdidLoad" section.

When any operation is used on the table, i.e. commitEditingStyle, i resave the values into my plist.

When changing viewcontroller, then returning to my uitableview, im expecting the list to be my new list, but it reverts back to the list prior to any editing (via swipe to delete or other method).

Within the UIviewtable.m file:

-(void)readThePlist
{
AddWeight *readlist =[[AddWeight alloc]init];
[readlist readPlist];
}
-(void)writeThePlist
{
AddWeight *writelist =[[AddWeight alloc]init];
[writelist writePlist];
}

- (void)viewDidUnload
{
[self dismissViewControllerAnimated:YES completion:nil];
[self writeThePlist];
[super viewDidUnload];
}



- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    HistoryItem *item = [weightsArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"EditItem" sender:item];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryItem"];

cell.textLabel.text = [weightsArray objectAtIndex:indexPath.row];  
cell.detailTextLabel.text = [datesArray objectAtIndex:indexPath.row];    

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [weightsArray removeObjectAtIndex:indexPath.row];
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)addItemViewControllerDidCancel:(AddItemViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addItemViewController:(AddItemViewController *)controller didFinishAddingItem:(HistoryItem *)item
{
    int newRowIndex = [weightsArray count];
[weightsArray addObject:item];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addItemViewController:(AddItemViewController *)controller didFinishEditingItem:(HistoryItem *)item
{
int index = [weightsArray indexOfObject:item];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self configureTextForCell:cell withHistoryItems:item];
[self dismissViewControllerAnimated:YES completion:nil];
} 

My read and write to file sections are in the AddWeight.m:

-(NSString *) dataFilePath
{ NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [path objectAtIndex:0]; return [documentDirectory stringByAppendingPathComponent:@"WeightsList.plist"];
}

- (void)writePlist
{
[weightsArray writeToFile:[self dataFilePath] atomically:YES];
}

-(void)readPlist
{ 
NSString *filePath = [self dataFilePath]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    { 
    weightsArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];; 
    NSLog(@"%@\n",weightsArray); 
    NSLog(@"%@\n", filePath); 
    } 
}
  • in the ViewdidUnload section, is it because i dismiss the viewcontroller before writing the plist?`- (void)viewDidUnload { [self dismissViewControllerAnimated:YES completion:nil]; [self writeThePlist]; [super viewDidUnload]; }` – Jonnybellman Jun 01 '12 at 08:28
  • Does no one have any idea how to help? :( – Jonnybellman Jun 01 '12 at 23:15

0 Answers0