2013-04-02 19 views
0

使用UITableViewController,其中我顯示了一些基於生產年份排序的產品。每當用戶點擊一行(每行代表一個產品),就會顯示一個新的視圖來編輯同一產品。UITableViewController沒有更新內容是我應用手動排序

所以保存後如果用戶改變了製造年份,那麼它應該顯示在主表視圖中適當的位置(即相應地重新排序)。 要完成此操作,我在...中調用sort()方法。這裏sort方法返回NSMutableArray類型的對象。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Sort the records based on number of days. 
    sortedProducts = [DTOConverter sort:unsortedProducts]; 

    return [sortedProducts count]; 
} 

但問題是,在表視圖我仍然有特定的行中的老值,直到我蹦蹦跳跳整個視圖,然後它被填充了新的更新後的值。

同樣的事情發生在新產品被添加的情況下。 請糾正我在哪裏做錯了,我相信這個問題可能與indexPath有關,但我不知道如何解決它。請幫忙。謝謝。

回答

2

要更新的tableview數據呼叫

[tableView reloadData]; 
+0

通過將reloadData放在我的numberOfRowsInSection方法中,此方法不斷調用,並在應用程序崩潰結束。 –

+0

它不應該在那裏。沒有行是一個函數要求沒有行的排序和其他事情應該在外面完成,它將在排序後調用 –

+0

noOfRows在部分應該只用於返回行數 –

0

你必須刷新表或各行使用下列方法之一:

  • reloadData
  • reloadRowsAtIndexPaths:withRowAnimation
  • reloadSections :withRowAnimation
  • reloadSectionIndexTitles

查看每個方法的文檔:UITableView Class Reference

注意:如果你只更新一排,不要罵[的tableView reloadData]。沒有必要重新加載表中的所有數據。

+0

realodData崩潰我的應用程序。試圖與reloadRowsAtIndexPath:withRowAnimation但具有相同的問題 - '代碼'(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分 { {/ /排序記錄基於天數。 sortedProducts = [DTOConverter sort:unsortedProducts]; NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[self.sortedProducts count] - 1 inSection:0]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; return [sortedProducts count]; }'code' –

+0

你從哪裏調用reloadData? – iOSGuru248

+0

在方法 - (NSInteger)tableView:(UITableView)tableView numberOfRowsInSection:(NSInteger)部分 –

0

您可能必須在您從tableview加載的新視圖中設置委託,然後設置您的tableviewcontroller以實現該新viewcontroller的委託方法。然後調用從該委託方法重新加載數據這是一個委託的示例:Delegate example