我和朋友正在开发一个购物清单应用程序并且在使用[tableView reloadData];时遇到了一些麻烦。我们将我们的杂货清单存储在plists中,并有一个添加项子视图。当您离开该子视图返回列表时,即使我们正在更新plist并在viewWillAppear中调用reloadData,该列表也不包含新项。它确实有效;如果我们导航回所有列表的主列表,然后返回到我们刚刚添加项目的特定列表,则项目会显示出来。同样,我们希望通过选择行并在项目旁边显示复选标记来从列表中删除项目。尽管我们在didSelectRowAtIndexPath中调用reloadData,但是当选择行时列表不会自动更新。同样,如果我们导航回主列表然后返回到单个列表,则会显示更改。我已经检查过,nib文件中的委托和dataSource正确链接到文件所有者。
以下是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");
}
任何帮助将不胜感激!提前谢谢!
答案 0 :(得分:1)
只需要:
[self.tableView reloadData];