2014-01-21 33 views
0

我試圖使用delegate從detailviewcontroller的masterviewcontroller中更新tableview。我從委託方法調用了reloadData,但它沒有做任何事情。我仍然無法解決這個問題。self.tableView reloadData在UISplitViewController主細節應用程序中不起作用

這是MasterViewController我的委託方法

- (void)updateScore:(DetailViewController *)controller withScore:(NSUInteger)score { 

     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:_selectedIndexPath]; 

     NSLog(@"%@", cell.detailTextLabel.text); 

     cell.detailTextLabel.text = [NSString stringWithFormat:@"Best score: %lu", (unsigned long)score]; 

     [self.tableView reloadData]; 

     NSLog(@"%@", cell.detailTextLabel.text); 

} 

從的NSLog的cell.detailTextLabel.text已更新,但您需要確保的tableview不刷新

感謝

回答

4

您的視圖控制器是tableview委託和數據源

如果您正在使用故事板,在連接檢查器下,您的ta bleview需要您的視圖控制器設置爲數據源和委託

,如果你只想做你的viewcontroller.m文件中的viewDidLoad方法,您可以使用這些線

self.tableView.delegate = self; 
self.tableView.dataSource = self; 
+0

感謝您的答覆, 但我已經做到了。因爲我需要從馬西德威數據源和委託方法,以顯示對detailviewcontroller數據的詳細信息 – taylorSWIFT

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     //other init 
    } 

    if(_selectedIndexPath.row == indexPath.row && _selectedIndexPath.section == indexPath.section){ 
     cell.detailTextLabel.text = [NSString stringWithFormat:@"Best score: %lu", (unsigned long)score]; 
    } 

} 

你可以嘗試移動代碼cellForRowAtIndexPath然後tableView reloadData

0
  1. [self.tableView reloadData]更新表中的所有可見單元格。它叫numberOfSectionsInTableView,numberOfRowsInSection,cellForRowAtIndexPath等等。換句話說:它完全更新表格。
  2. 設置單元格內容的唯一正確方法是將其設置爲cellForRowAtIndexPath。代碼:

    if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //other init} 
    

    如果您使用故事板,則永遠不會從iOS 6調用。因此,與您的代碼

  1. 使用不正確的方法來設置單元格的內容。
  2. [self.tableView reloadData]清除所有設置。

解決方案:

  1. 保存score__strong ivarproperty。請致電[self.tableView reloadData]。它在適當的時候調用cellForRowAtIndexPath
  2. cellForRowAtIndexPath方法中設置新的score

建議:用途:

dispatch_async(dispatch_get_main_queue(), ^(){ [self.tableView reloadData]; }); 

從委託快回來,不要等到表進行了更新。

相關問題