ios - 如何从另一个 View Controller 将值插入 UITableViewCell

标签 ios objective-c uitableview uinavigationcontroller

我有一个 UITableView里面有10个细胞。我有一个 UIButton & UILabel在所有 10 个细胞内。现在如果我选择 UIButton在第二个单元格中,我将导航到另一个页面并选择一些值(字符串值)。现在,我希望将该字符串放在 UILabel 中。的第 2 个单元格。同样,如果我选择 UIButton在第 5 个单元格上,我将导航到另一个页面,选择一些字符串,该字符串必须放在 UILabel 中第 5 个细胞。如何在 UILabel 中插入值那个特定的细胞。

想法受到赞赏。

最佳答案

我建议将第二个 View Controller 设置为包含第一个 View Controller 的实例,以便您可以从第二个 View 操作第一个 View Controller 的数据数组。

例如,在 FirstViewController 的部分中在哪里创建 SecondViewController执行推送(因为正如您在评论中所说,您正在使用 UINavigationController 推送 View Controller ),也使 SecondViewController包含 FirstViewController 的实例.
FirstViewController米:

// Method to handle the push transition
- (void)buttonPressed:(UIButton*)button {

    // Set the view controller's `selectedRow` property
    // to contain the selected row number as contained in 
    // the button tag in this particular scenario
    self.selectedRow = (int)button.tag;

    // Create an instance of SecondViewController
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    // Pass the view controller along by setting
    // the secondViewController's firstViewController
    // property to contain self, i.e. the current
    // instance of the FirstViewController
    secondViewController.firstViewController = self;

    // Push to the SecondViewController
    [self.navigationController pushViewController:secondViewController animated:YES];
}

SecondViewController 的 .h 中, 添加 firstViewController属性,使其可从 FirstViewController 公开访问.
SecondViewController 。H:
#import "FirstViewController.h"

@interface SecondViewController : UIViewController

@property (weak, nonatomic) FirstViewController *firstViewController;

然后在 SecondViewController 的 .m 中,您可以在 FirstViewController 中操作充当表数据源的数组根据需要,在弹出 View 之前。
SecondViewController米:
// Method called after the string is selected
- (void)stringSelected:(NSString*)string {

    // Update the array to contain the string
    // (or do whatever manipulation to whatever data
    // structure is fitting in your particular case)
    [self.firstViewController.array replaceObjectAtIndex:self.firstViewController.selectedRow withObject:string];

    // To go back to the previous view, pop the view controller
    [self.navigationController popViewControllerAnimated:YES];
}

此外,您还需要 UITableView的数据源arrayselectedRow要包含在 FirstViewController 的 .h 中的属性所以它可以从 SecondViewController 公开访问.
FirstViewController 。H:
@property (weak, nonatomic) NSMutableArray *array;
@property (nonatomic) int selectedRow;

FirstViewController 的 .m 中您要确保在从另一个 View 返回时重新加载表数据,因此 UITableView反射(reflect)更新的数据。
FirstViewController米:
- (void)viewWillAppear:(BOOL)animated {
    [self.table reloadData];
}

关于ios - 如何从另一个 View Controller 将值插入 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27497601/

相关文章:

objective-c - Objective-C 中字符串与数组元素的串联

ios - 用于共享屏幕截图的事件 Controller 显示为空

ios - 快速观察者不起作用

objective-c - 释放 GCD 调度队列属性的正确方法是什么?

Swift 中的 UITableViewCell

ios - Swift - UITableView 显示奇怪的白色单元格

ios - 是否可以在 iOS 中的一定时间间隔后显示 iAd

ios - Swizzling SKStoreProductViewController viewWillAppear 或 viewDidAppear 不工作

objective-c - @"%@ 在 Objective-C 中?

swift - 表格 View 卡住了很多