2011-03-27 91 views
1

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

} 

感谢所有谁可以帮助

回答

4

你似乎没有打电话UITableViewreloadData方法在任何时候你'我们进行了删除。

因此,加入...

[tableView reloadData]; 

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

+0

完全正是我想要的:)干杯兄弟 – user393273 2011-03-27 17:57:16

3

我对此有两种思想:

1)您可以删除行中的commitEditingStyle:...方法。下面是苹果的“位置”的示例代码

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

一个例子搜索commitEditingStyle的文档:你会发现其他的例子。

2)它看起来像你已经添加了代码viewDidLoad配置表视图。这将是一个好主意,删除代码,并把它放在一个单独的方法如

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

,然后调用它在任何你需要它。直接调用viewDidLoad并不是一个好主意,因为它通常会执行其他一些操作,比如'[super viewDidLoad]',它们不需要更新表视图。

+0

我有一个不同的问题,在删除行/调用'[reloadData]'后不久,表会挂起(不滚动)。使用'deleteRowsAtIndexPaths'代替解决这个问题! – canhazbits 2014-07-01 04:43:46