友人と一緒に買い物リスト アプリを開発していて、[tableView reloadData]; で問題が発生しています。食料品のリストを plists に保存し、アイテムの追加サブビューを持っています。そのサブビューを離れてリストに戻ると、plist を更新して viewWillAppear で reloadData を呼び出しているにもかかわらず、リストに新しい項目が含まれていません。ただし、機能します。すべてのリストのマスター リストに戻り、アイテムを追加したばかりの特定のリストに戻ると、アイテムが表示されます。同様に、行を選択して項目の横にチェックマークを表示するだけで、リストから項目を削除したいと考えています。didSelectRowAtIndexPath で reloadData を呼び出しても、行が選択されてもリストは自動的に更新されません。また、マスター リストに戻ってから個別リストに戻ると、変更が表示されます。確認したところ、nib ファイルのデリゲートとデータソースがファイルの所有者に正しくリンクされています。
viewWillAppear のコードは次のとおりです。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// THIS IS WHERE YOU PASS THE VARIABLE
iGrocerAppDelegate *passTest = [[UIApplication sharedApplication] delegate];
NSString *listName = [passTest passList];
[passTest setPassList:listName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullListName = [NSString stringWithFormat:@"%@.plist", listName];
NSString *file = [[paths lastObject] stringByAppendingPathComponent:fullListName];
self.list = [[NSArray alloc] initWithContentsOfFile:file];
[listTableView reloadData];
}
および didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:
indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
int row = [indexPath row];
NSString *deleteItem = [self.list objectAtIndex:row];
[self.itemsToDelete addObject:deleteItem];
// RECEIVE PASSED VARIABLE
iGrocerAppDelegate *test = (iGrocerAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *listName = [test passList];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullListName = [NSString stringWithFormat:@"%@.plist", listName];
NSString *file = [[paths lastObject] stringByAppendingPathComponent:fullListName];
//NSMutableArray *groceryLists = [[NSMutableArray alloc] initWithContentsOfFile:file];
//NSMutableArray *groceryList1 = [[NSMutableArray alloc] initWithContentsOfFile:file];
NSMutableArray *groceryList = [[NSMutableArray alloc] initWithContentsOfFile:file];
NSMutableArray *removeFromList = self.itemsToDelete;
int itemCount = [removeFromList count];
NSLog(@"Count: %d", itemCount);
for(int i=0; i < itemCount; i++) {
NSString *item = [removeFromList objectAtIndex:i];
NSLog(@"%@", item);
[groceryList removeObject:item];
NSLog(@"Middle ARRAY: %@", groceryList);
//[groceryList addObjectsFromArray: addToList];
}
NSLog(@"FINAL ARRAY: %@", groceryList);
if(![groceryList writeToFile:file atomically:YES]) {
NSLog(@"unsuccessful");
} else {
NSLog(@"successful");
}
NSLog(@"file name: %@",file);
self.list = [[NSArray alloc] initWithContentsOfFile:file];
NSLog(@"updated grocery list: %@", self.list);
[listTableView beginUpdates];
//[listTableView reloadData];
[listTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[listTableView endUpdates];
printf("list table view reloaded");
}
どんな助けでも大歓迎です!前もって感謝します!