【问题标题】:Not able to update TableCelView in UITableViewController when Navigating back from a UIViewController从 UIViewController 导航返回时无法更新 UITableViewController 中的 TableCelView
【发布时间】:2012-08-06 16:40:52
【问题描述】:

我有一个与UITableViewController 链接的导航视图控制器。 在表视图控制器中,我有静态单元格。我有表格视图单元格的正确标签格式。单击UITableViewCell 时,我将导航到UIViewController,然后当我导航回UITableViewController 时,我想更新UITableViewCell 上的详细信息标签。

我可以将 thenter code heree UIViewController 的值传递给 UITableViewcontroller。我已经使用NSLog 验证了这一点。但我无法更新单元格中的详细标签。

我使用了-(void) tableView:(UITableView *) tableView didDeselectRowAtIndexPath,但这对我没有帮助,因为只有当我单击另一个表格视图单元格时,表格视图单元格才会更新。我希望当我导航回UITableViewController时自动进行此更新

请检查以下链接 http://www.icodeblog.com/wp-content/uploads/2011/10/navigation_interface.jpeg

我想要类似上图的东西

如何做到这一点。

代码更新

@interface designTableViewController ()

@end

@implementation designTableViewController
{
    glimmpseliteAppDelegate *appDelegate;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    appDelegate = [[UIApplication sharedApplication]delegate];
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

-(void)viewWillAppear:(BOOL)animated
{

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 8;
}

#pragma mark - Table view delegate

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 1)
    {
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        UIViewController *uiViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"designSolvingForViewController"];
        [self.navigationController pushViewController:uiViewController animated:YES];
        tb.detailTextLabel.text = appDelegate.solvingFor;


    }
    if(indexPath.row == 2)
    {
        UIViewController *uiViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"designTypeIErrorRate"];
        [self.navigationController pushViewController:uiViewController animated:YES];
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        tb.detailTextLabel.text = appDelegate.typeIError;
    }
    if(indexPath.row == 3)
    {
        UIViewController *uiViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"designNumberOfGroups"];
        [self.navigationController pushViewController:uiViewController animated:YES];
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        tb.detailTextLabel.text = appDelegate.numberOfGroups;
    }

    if(indexPath.row == 4)
    {
        UIViewController *uiViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"designRelativeGroupSize"];
        [self.navigationController pushViewController:uiViewController animated:YES];
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        tb.detailTextLabel.text = appDelegate.relativeGroupSize;
    }

    if(indexPath.row == 5)
    {
        UIViewController *uiViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"designSmallestGroupSize"];
        [self.navigationController pushViewController:uiViewController animated:YES];
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        tb.detailTextLabel.text = appDelegate.smallestGroupSize;
    }

    if(indexPath.row == 6)
    {
        UIViewController *uiViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"designMeansAndVariance"];
        [self.navigationController pushViewController:uiViewController animated:YES];
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        tb.detailTextLabel.text = appDelegate.meansAndVariance;
    }
}

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 1)
    {
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        tb.detailTextLabel.text = appDelegate.solvingFor;
    }

    if(indexPath.row == 2)
    {
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        //tb.textLabel.text =@"Sample";
        tb.detailTextLabel.text = appDelegate.typeIError;
    }

    if(indexPath.row == 3)
    {
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        //tb.textLabel.text =@"Sample";
        tb.detailTextLabel.text = appDelegate.numberOfGroups;
    }


    if(indexPath.row == 4)
    {
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        //tb.textLabel.text =@"Sample";
        tb.detailTextLabel.text = appDelegate.relativeGroupSize;
    }

    if(indexPath.row == 5)
    {
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        //tb.textLabel.text =@"Sample";
        tb.detailTextLabel.text = appDelegate.smallestGroupSize;
    }

    if(indexPath.row == 6)
    {
        UITableViewCell *tb = [tableView cellForRowAtIndexPath:indexPath];
        //tb.textLabel.text =@"Sample";
        tb.detailTextLabel.text = appDelegate.meansAndVariance;
    }
}

@end

第一个表格单元格只是一个有按钮的单元格。我有一个用于那个按钮的推送 UIViewController。

【问题讨论】:

  • 请发布您的 cellForRowAtIndexPath,它似​​乎是导致问题的原因..

标签: objective-c ios ios5 uitableview


【解决方案1】:

在 viewWillAppear 中:使用 [tableView reloadData]。

- (void)viewWillAppear:(BOOL)animated
{
     [tableView reloadData];
}

【讨论】:

  • 那么你的 cellForRow... 方法没有正确实现。
  • 我已经为我的 tableViewController 添加了代码,请检查
【解决方案2】:

首先确保您的模型已更新,以便您的 cellForRowAtIndexPath: 将返回正确的单元格。

然后使用以下方法之一更新viewWillAppear 中的单元格:

- (void)reloadData
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

【讨论】:

  • 是的,我的 cellForRowAtIndexPath: 将返回正确的单元格。但是您能否更详细地说明如何使用您提到的方法,因为我是 iOS 开发新手
  • [self reloadRowsAtIndexPaths: withRowAnimation:];如何获取 IndexPath 和 UITableViewRowAnimation
  • 如果您不知道需要重新加载的 indexPath,那么只需执行 reloadData。 indexPath 将是具有新信息的单元格的路径。您可以在推送新视图之前将该信息保存在 didSelectRowAtIndexPath: 中,或者您可以将 TableViewController 设置为不自动清除选择,您可以自己清除它并捕获选择了哪个单元格。
  • 我尝试执行 viewWillAppear 并调用不起作用的重新加载
  • 或许可以拨打viewDidAppear。否则,您的 cellForRow... 将无法正确返回。
【解决方案3】:

您似乎并不完全了解 UITableView 委托和数据源的实现。一些提示:


  1. 由于您使用的是情节提要,控制从静态表格单元格拖动到您希望在选择单元格时出现的视图控制器,这将自动创建一个 segue(您可能已经意识到这一点),这样您就不会无需担心实现 didDeselectRowAtIndexPath,您可以将每个静态单元格连接到不同的 VC。
  2. 您的 UITableViewController 类中可能已经有一个数据源,例如您的 detailLabel(s) 的 NSString 标题数组。在 tableView:CellForRowAtIndex: 你会想要编写类似的代码

cell.detailLabel.text = [dataArray objectAtIndex:indexPath.row];

3) 在更改这些标题的视图控制器中,您希望建立与 UITableViewController 的委托连接,该连接使用新值更新 dataArray。然后在 UITableViewController 类中重写 setDataArray:(NSArray *)array 如下:

- (void)setDataArray:(NSArray *)array {
    _dataArray = array;  
   [self.tableView reloadData];

}

【讨论】:

    【解决方案4】:

    您必须使用 tableView:CellForRowAtIndex: 来更新单元格的内容,就像您最初创建单元格一样。当应用返回列表时(在另一个视图中更新设置后) tableView:CellForRowAtIndex: 将被自动调用。

    更正:tableView:cellForRowAtIndex 不会被自动调用。在你看来WillAppear:放

    [tableView ReloadData];
    

    【讨论】:

    • 试着把它放在 viewDidAppear: 中。如果这仍然不起作用,那么您没有正确实施 tableView:cellForRowAtIndexPath: 。给我们看一些代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多