過去の入力値のリストを表示する単純なテーブルビューがあります。このリストは 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);
}
}