self.tableView reloadData在UISplitViewController主详细信息应用程序中不起作用

时间:2014-01-21 01:38:59

标签: ios objective-c ipad tableview uisplitviewcontroller

我试图使用delegate从detailviewcontroller更新masterviewcontroller中的tableview。我从委托方法中调用了reloadData,但它没有工作。我仍然无法解决这个问题。

这是我在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没有重新加载

感谢

3 个答案:

答案 0 :(得分:4)

您需要确保您的视图控制器是tableview委托和数据源

如果您使用的是故事板,请在连接检查器下,您的tableview需要将您的视图控制器设置为dataSource和delegate

如果您想在viewDidLoad方法的viewcontroller.m文件中执行此操作,可以使用这些行

self.tableView.delegate = self;
self.tableView.dataSource = self;

答案 1 :(得分: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

答案 2 :(得分:0)

  1. [self.tableView reloadData]更新表格中的所有可见单元格。它会调用numberOfSectionsInTableViewnumberOfRowsInSectioncellForRowAtIndexPath等等。换句话说:它完全更新了表格。
  2. 设置单元格内容的唯一正确方法是在cellForRowAtIndexPath中设置它。代码:

    if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //other init}
    
    如果您使用故事板,

    自iOS 6以来永远不会被调用。

  3. 所以用您的代码:

    1. 使用不正确的方式设置单元格内容。
    2. 使用[self.tableView reloadData]清除所有设置。
    3. 解决方案:

      1. score保存在__strong ivarproperty
      2. 致电[self.tableView reloadData]。它会在适当的时候调用cellForRowAtIndexPath
      3. score方法中设置新的cellForRowAtIndexPath
      4. 建议:使用:

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

        从代理快速返回,而不是等到表更新。

相关问题