从 UITableView 中删除行
这是我的删除代码,它工作正常,但您必须打开另一个选项卡,然后再次单击此选项卡才能删除该项目。由于这不是正常的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您要么需要调用 reloadData 或 –deleteRowsAtIndexPaths:withRowAnimation:。
Well, you either need to call reloadData or – deleteRowsAtIndexPaths:withRowAnimation:.
您似乎没有调用 UITableView执行删除后随时调用
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...
...to the bottom of your
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
method should solve the problem.我对此有两个想法:
1)您可以删除 commitEditingStyle:... 方法中的行。以下是 Apple 的“Locations”示例代码中的示例。
在文档中搜索 commitEditingStyle:,您将找到其他示例。
2)看起来您已经向 viewDidLoad 添加了代码来配置表视图。删除该代码并将其放入单独的方法中是一个好主意,例如
然后在需要的地方调用它。直接调用 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
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
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.