1

这是我删除的代码,它工作正常,但您必须打开另一个选项卡,然后再次单击此选项卡才能删除项目。由于这不是普通的 UITableView,您不能只从数组中删除然后更新表。所以有人可以帮我刷新视图。下面的代码段也不起作用:

// Reload the view completely
if ([self isViewLoaded]) {
    self.view=nil;
    [self viewDidLoad];
}

这是我的删除代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete){
        // Delete song from list & disc \\

        //Remove from arrays
        [self.Writer removeObjectAtIndex:[indexPath row]];
        [self.Book removeObjectAtIndex:[indexPath row]];
        [self.BookImageId removeObjectAtIndex:[indexPath row]];

        NSString *TempWriter = [self.ArtistNamesArray componentsJoinedByString:@","];
        NSString *TempBook = [self.SongNamesArray componentsJoinedByString:@","];
        NSString *TempBookImageId = [self.YoutubeIDSArray componentsJoinedByString:@","];

        TempWriter = [TempWriter substringToIndex:[TempArtistNames length] - 1];
        TempBook = [TempBook substringToIndex:[TempSongNames length] - 1];
        TempBookImageId = [TempBookImageId substringToIndex:[TempYouTube length] - 1];


        //Save Details
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%@,", TempBook] forKey:@"BookName"];
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%@,", TempWriter] forKey:@"WriterName"];
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%@,", TempBookImageId] forKey:@"ImageId"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }


}

我的 ViewWillAppear 代码是:

- (void) viewWillAppear: (BOOL) animated
{
    // Reload the view completely
    if ([self isViewLoaded]) {
        self.view=nil;
        [self viewDidLoad];
    }

}

感谢所有愿意提供帮助的人

4

3 回答 3

7

好吧,你要么需要调用reloadData要么- deleteRowsAtIndexPaths:withRowAnimation:

于 2011-03-27T17:47:29.390 回答
4

执行删除后,您似乎不会在任何时候调用UITableView方法。 reloadData

因此,添加...

[tableView reloadData];

...到你的- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath方法的底部应该可以解决问题。

于 2011-03-27T17:46:57.327 回答
3

对此我有两个想法:

1)您可以删除 commitEditingStyle:... 方法中的行。这是 Apple 的“Locations”示例代码中的一个示例

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

搜索 commitEditingStyle: 的文档,您会发现其他示例。

2) 看起来您已经向 viewDidLoad 添加了代码来配置表格视图。删除该代码并将其放在单独的方法中是个好主意,例如

- (void) updateMyTableView
{
     // Code to reload the table view.
}

然后在需要的地方调用它。直接调用 viewDidLoad 并不是一个好主意,因为它通常会执行其他不需要更新表视图的事情,例如“[super viewDidLoad]”。

于 2011-03-27T18:30:00.580 回答