0

我和一个朋友正在开发一个购物清单应用程序,但在使用 [tableView reloadData]; 时遇到了一些问题。我们将杂货清单存储在 plist 中,并有一个添加项目子视图。当您离开该子视图返回列表时,即使我们正在更新 plist 并在 viewWillAppear 中调用 reloadData,列表也不包含新项目。它确实有效;如果我们导航回所有列表的主列表,然后返回到我们刚刚添加项目的特定列表,那么这些项目就会显示出来。同样,我们想从列表中删除项目,只需选择行并在项目旁边显示一个复选标记。尽管我们在DidSelectRowatIndExpath中也调用ReloDdata,但该列表不会自动更新行时。再次,如果我们导航回主列表然后返回到单个列表,则会显示更改。我已经检查过了,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");
}

任何帮助将非常感激!提前致谢!

4

1 回答 1

1

解决了。

它只需要:

[self.tableView reloadData];
于 2011-05-03T02:42:29.650 回答