简体   繁体   English

从两个不同的视图控制器更新表视图单元

[英]Update a Table View Cell from two different view controllers

I am new to IOS and i was just working on UITableView . 我是IOS的新手,我只是在UITableView工作。 I want to populate data in table view from two different View Controllers. 我想从两个不同的视图控制器填充表视图中的数据。 I want to update cell.textLabel.text from View Controller say viewController1 and cell.detailTextLabel.text from another View Controller say viewController2 . 我想更新cell.textLabel.text从视图控制器说viewController1cell.detailTextLabel.text从另一个视图控制器说viewController2

I have a barbutton at the top right of the tableView and clicking on which it navigates to View Controller A. I have a text label in View Controller A and it returns what ever user has entered to tableView. 我在tableView右上角有一个按钮,单击它会导航到View ControllerA。在View Controller A中有一个文本标签,它会返回用户输入到tableView的内容。 When i click the cell, it navigates to View Controller B. I also have a text label in View Controller B. 当我单击该单元格时,它导航到View ControllerB。我在View Controller B中也有一个文本标签。

Here is my tableView datasource method 这是我的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;

}

where Names is a model class of NSObject and lists is an NSArray in tableView . 其中NamesNSObject的模型类,列表是tableViewNSArray

It works fine if I update cell.textlabel.text and cell.detailtextlable.text from a single View Controller either View Controller1 or View Controller2. 如果我从单个View Controller View Controller1或View Controller2更新cell.textlabel.textcell.detailtextlable.text ,它会很好地工作。

I can get data from viewController2 but where should I have cell.detailTextLabel.text ? 我可以从viewController2获取数据,但应该在哪里有cell.detailTextLabel.text

  1. coding conventions... http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java is a good orientation. 编码约定... http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java是一个很好的方向。

  2. It seems like simple parameter handling. 似乎简单的参数处理。 You can share the information between the views in the init process. 您可以在初始化过程中在视图之间共享信息。

ie

// 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];
}

if there are further problems, please give some more information where exactly ;) 如果还有其他问题,请在确切位置提供更多信息;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM