There is a table with a list of words and checkmarks. If a word is selected, it is added to the array, which is then written to the plist dictionary. I have added and removed only one line. If I select another line, it overwrites the old one. Googled half a day, did not understand what to do, tried to copy the array - did not work. Here is my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; bool isSelected = (cell.accessoryType == UITableViewCellAccessoryCheckmark); NSString *path = [DOCUMENTS stringByAppendingPathComponent:@"userData.plist"]; NSMutableDictionary *data = [NSMutableDictionary dictionaryWithContentsOfFile:path]; NSMutableArray *newData = [[NSMutableArray alloc] init]; if (isSelected) { cell.accessoryType = UITableViewCellAccessoryNone; } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; [newData addObject:cell.textLabel.text]; } [data setObject:newData forKey:@"PersonalQualities"]; NSData *dataToPlist = [NSPropertyListSerialization dataWithPropertyList:data format:NSPropertyListXMLFormat_v1_0 options:0 error:nil]; [dataToPlist writeToFile:path atomically:YES]; } 

    1 answer 1

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; bool isSelected = (cell.accessoryType == UITableViewCellAccessoryCheckmark); NSString *path = [DOCUMENTS stringByAppendingPathComponent:@"userData.plist"]; NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path]; cell.accessoryType=isSelected? UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark; NSArray *oldData=dataDictionary[@"PersonalQualities"]; NSMutableArray *newData=[NSMutableArray new]; if (oldData!=nil) [newData addObjectsFromArray:oldData]; [newData addObject:cell.textLabel.text]; [dataDictionary setObject:newData forKey:@"PersonalQualities"]; NSData *dataToPlist = [NSPropertyListSerialization dataWithPropertyList:dataDictionary format:NSPropertyListXMLFormat_v1_0 options:0 error:nil]; [dataToPlist writeToFile:path atomically:YES]; 

    }

    • It works, but by half. New lines are added to the array, and not overwritten into one line as before. When you put a tick in the list - the line is added to the array, put a tick on the next line - the next line is added to the array, OK. But when you remove the check mark, this line is not removed from the array, but duplicated. - Alexander Yakovlev
    • Happened! NSMutableArray *newData=[[NSMutableArray alloc] initWithArray:data[@"PersonalQualities"]]; if (isSelected) { [newData removeObject:cell.textLabel.text]; } else { [newData addObject:cell.textLabel.text]; } NSMutableArray *newData=[[NSMutableArray alloc] initWithArray:data[@"PersonalQualities"]]; if (isSelected) { [newData removeObject:cell.textLabel.text]; } else { [newData addObject:cell.textLabel.text]; } - Alexander Yakovlev