我有一个 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
的数据源array
和 selectedRow
要包含在 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/