私は、このコードを介して plist からの配列キーによって設定される UITableView を持っています:
-(void)viewWillAppear:(BOOL)animated
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];
viewerKeys = [dictionary allKeys];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ViewerName"];
UILabel *label = (UILabel *)[cell viewWithTag:1000];
label.text = [viewerKeys objectAtIndex:indexPath.row];
return cell;
}
テーブルビューの各行をスワイプして削除できるようにしようとしています。もちろん、これは行とplistの配列を削除する必要があることを意味します。これは私がこれまでに試したことです:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];
NSString *key = [viewerKeys objectAtIndex:indexPath.row];
[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
ただし、データを plist に書き戻しておらず、次のエラーが発生します。
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
これは私のplistがどのように見えるかです:
<plist version="1.0">
<dict>
<key>(null)</key>
<array>
<string>Username</string>
<string>Password</string>
<string>http://www.google.com</string>
<string>/whatever</string>
</array>
<key>Hello</key>
<array>
<string>admin</string>
<string></string>
<string>https://www.whatever.com</string>
<string>/things</string>
</array>
</dict>
</plist>
どんな助けでも素晴らしいでしょう!