从 UITableView 中删除行

发布于 2024-10-26 23:39:17 字数 2028 浏览 5 评论 0原文

这是我的删除代码,它工作正常,但您必须打开另一个选项卡,然后再次单击此选项卡才能删除该项目。由于这不是正常的 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];
    }

}

感谢所有提供帮助的人

Here is the code for my delete, it works fine but you have to open another tab and then click this tab again for the item to be removed. As this is not the normal UITableView you cannot just remove from array and then update table. so could someone please help me refresh the view. Also the snippet below does not work:

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

Here is my delete code:

- (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];

    }


}

and my ViewWillAppear code is:

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

}

Thanks for all who will help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘过的浮云 2024-11-02 23:39:17

您似乎没有调用 UITableView执行删除后随时调用 reloadData 方法。

因此,添加...

[tableView reloadData];

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

You don't appear to be calling the UITableView reloadData method at any point after you've carried out the deletion.

As such, adding...

[tableView reloadData];

...to the bottom of your - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath method should solve the problem.

笑叹一世浮沉 2024-11-02 23:39:17

我对此有两个想法:

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]”。

I have two thoughts on this:

1) You can delete the row in the commitEditingStyle:... method. Here's an example from Apple's "Locations" sample code

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

Search the documentation for commitEditingStyle: and you'll find other examples.

2) It looks like you've added code to viewDidLoad to configure the table view. It would be a good idea to remove that code and put it in a separate method such as

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

and then calling it where ever you need it. Calling viewDidLoad directly is not such a good idea because it usually does other things such as '[super viewDidLoad]' which aren't needed to update the table view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文