2014-01-12 78 views
5

我正在iOS中創建一個基於練習的應用程序。我想包括的是我的最愛列表。就目前來看,我有一個練習庫,您可以點擊一個按鈕並將其添加到通過導航控制器訪問的收藏夾列表中。我能夠正確地將標題傳遞給商店,然後將商店放在收藏夾表格視圖中。我可以根據表格視圖單元格的標題加載特定的exerciseViewController並在運行之間保存。不過,我嘗試刪除練習時遇到問題。我一直在掙扎幾個小時。如何讓UITableView正確地重新加載?

這是情景:

我已經添加了兩個練習我的最愛:exercise A這是第一排,exercise B這是第二個。但是,當訪問收藏夾列表時,我選擇exercise A來查看其詳細信息,並決定不公開它,然後導航(返回)至收藏夾列表,但未正確更新。雖然它正確加載行數(現在爲1),但它仍顯示exercise A標題(它應顯示exercise B標題)。但是,當我一直導航到主頁並選擇收藏夾時,它會正確顯示 - 現在只顯示exercise B標題。我曾嘗試致電[[self tableview] reload]viewWillAppear(這似乎是最常見的答案),編寫通知,協議等,並且仍然存在此問題。唯一能正常工作的時間是通過主頁訪問收藏夾列表並調用viewDidLoad

如何在表格顯示時正確顯示錶格而不必加載?

收藏泰伯維

FavsVC.m 
- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [self.tableView reloadData]; 
    NSLog(@"APPEARING"); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
     NSArray *fav = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"]; 
     NSString *name = [fav objectAtIndex:indexPath.row]; 
     cell.textLabel.text = name; 
    } 
    return cell; 
} 

//This the method is in my store, and gets called when I click on an exercise to add or delete. 
- (id)addToFavorites:(NSString *)title 
{ 
    favoriteTitles = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"]; 
    if ([favoriteTitles containsObject:title]) { 
     NSLog(@"exercise removed"); 
     //[favoriteTitles removeObject:title]; 

     NSMutableArray *newArray = [NSMutableArray arrayWithArray:favoriteTitles]; 
     [newArray removeObject:title]; 
     [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"Favorites"]; //newArray 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } else { 
     NSLog(@"exercise added"); 
     //[favoriteTitles addObject:title]; 

     NSMutableArray *newArray = [NSMutableArray arrayWithArray:favoriteTitles]; 
     [newArray addObject:title]; 
     [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"Favorites"]; //newArray 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 
    return self; 
} 

回答

5

你不創造新的細胞每一次,這就是爲什麼你最終獲取「錯誤的細胞」。您使用可重複使用的單元格,而不是每次都在cellForRowAtIndexPath中創建,而是進行了修改。原因在於UITableView僅存儲實際存儲在屏幕上的單元格以用於存儲目的。改用它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

     if (!cell) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
      // Only create a new cell if it's nil 
     } 
     // Now you have a reference to the cell. Set its variables. 
     NSArray *fav = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"]; 
     NSString *name = [fav objectAtIndex:indexPath.row]; 
     cell.textLabel.text = name; 
     return cell; 
    } 
+0

感謝您的幫助,這解決了我的問題。 – BradRS

+0

高興,但隨時把它作爲正確的問題。 – Martol1ni

0

想要通知您將要呈現視圖控制器嗎?只需使用UINavigationControllerDelegate即可。

// Somewhere in a table view controller 
self.navigationController.delegate = self; 
// ------------------------------------------------------- 
- (void)navigationController:(UINavigationController *)navigationController  
     willShowViewController:(UIViewController *)viewController 
        animated:(BOOL)animated 
{ 
    // Your code to update the table view 
} 

不要忘記設置您的表視圖控制器以符合UINavigationControllerDelegate

另一種選擇是在對相關內容進行一些更改後觸發表格提示的重新加載,在您的收藏夾視圖控制器中進行。

但我個人更喜歡第一種方法,因爲在這裏你有一個鬆耦合。

0

我認爲這個問題是如果沒有聲明導致已經取得刷新你不需要你如果不聲明你只需要從數據

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     UITableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 

//I would probably make a property for array... not create it here 
NSArray *fav = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"]; 
    if(fav){ 
     NSString *name = [fav objectAtIndex:indexPath.row]; 
    cell.textLabel.text = name; 
    } 
    return cell; 
} 

還你怎麼樣裝載表單元格解僱您的收藏夾視圖?

是你的viewWillAppear:方法被調用?

//檢查筆尖也默認應該是肯定的,但來自筆尖檢查你的清醒,如果你有一個

- (void)awakeFromNib { 

    [super awakeFromNib]; 

     self.clearsSelectionOnViewWillAppear = YES;  

}