2011-03-27 90 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