我有一个简单的表格视图,它显示历史输入值的列表。此列表保存在 plist 中,并在“ViewdidLoad”部分中加载。
当在表上使用任何操作时,即 commitEditingStyle,我将值重新保存到我的 plist 中。
当更改视图控制器,然后返回到我的 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);
}
}