【问题标题】:Update a Table View Cell from two different view controllers从两个不同的视图控制器更新表格视图单元
【发布时间】:2015-04-16 16:17:27
【问题描述】:

我是 IOS 新手,我刚刚在 UITableView 工作。我想从两个不同的视图控制器中填充表视图中的数据。我想从视图控制器更新cell.textLabel.text,比如viewController1,从另一个视图控制器更新cell.detailTextLabel.text,比如viewController2

我在tableView 的右上角有一个条形按钮,单击它可以导航到视图控制器 A。我在视图控制器 A 中有一个文本标签,它会将用户输入到 tableView 的内容返回。 当我单击单元格时,它会导航到 View Controller B。我在 View Controller B 中也有一个文本标签。

这是我的tableView 数据源方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    Names *namesToDisplay = [self.lists objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

    cell.textLabel.text = namesToDisplay.firstName;
    return cell;

}

其中NamesNSObject 的模型类,列表是tableView 中的NSArray

如果我从单个视图控制器(视图控制器 1 或视图控制器 2)更新 cell.textlabel.textcell.detailtextlable.text 效果很好。

我可以从 viewController2 获取数据,但我应该在哪里获取 cell.detailTextLabel.text

【问题讨论】:

  • 你现在解决这个问题了吗?答案有帮助吗?如果不是,请发表评论,如果是,请投票/接受;)

标签: ios objective-c uitableview tableviewcell


【解决方案1】:
  1. 编码约定...http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java 是一个很好的方向。

  2. 看起来像简单的参数处理。您可以在 init 进程中的视图之间共享信息。

// your details view (vc2) header
@class Names;

@interface MyDetailsViewController
{
    Names *names;
    // some variable declaration here
}
// maybe some properties...

- (id)initWithObject:(Names *)p_names;
// ...

@end

...

// your details view (vc2) main
- (id)initWithObject:(Names *)p_names
{
    if ((self = [super init]))
    {
        names = p_names; // so you have the data when the tableView is loading
    }
}

...

// your main view (vc1)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Names *namesToDisplay = [self.lists objectAtIndex:indexPath.row];
    MyDetailsViewController *viewController2 = [[MyDetailsViewController alloc] initWithObject:namesToDisplay];
    [self.navigationController pushViewController:viewController2 animated:YES];
}

如果还有其他问题,请提供更多信息;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多