plist应该加载和更新表,但仅适用于第一次加载

时间:2012-05-31 22:23:28

标签: uitableview plist save writetofile

我有一个简单的tableview,它显示了一个历史输入值列表。此列表保存在plist中,并加载到“ViewdidLoad”部分。

当在表上使用任何操作时,即commitEditingStyle,我将值重新保存到我的plist中。

当更改viewcontroller,然后返回到我的uitableview时,我希望列表成为我的新列表,但在任何编辑之前它会恢复到列表(通过滑动删除或其他方法)。

在UIviewtable.m文件中:

-(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];
} 

我对文件部分的读写是在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); 
    } 
}

0 个答案:

没有答案