[英]How to call a method from another view controller
我想弄清楚如何采取-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
, 方法并从另一个视图控制器调用它。
在LocationViewController.m 中
- (void)viewDidLoad {
[super viewDidLoad];
//save bar button item
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle: @"Save" style: UIBarButtonItemStylePlain target: self action: @selector(saveButtonPressed)];
self.navigationItem.rightBarButtonItem = saveButton;
[self performSelector:@selector(saveButtonPressed) withObject:nil afterDelay:0.25];
}
-(void)saveButtonPressed {
//I want to call the method here
}
在GameDetailsTableViewController.m 中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifer1 = @"GameDetailsLocationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer1];
cell.textLabel.text = @"hello world"
return cell;
}
我是 IOS 的初学者,我将不胜感激任何帮助。
您不能调用cellForRowAtIndexPath
,但您可以从另一个类触发 tableview 的数据源方法。 就是这样
假设你想从 B 类的方法someMethod
触发 A 类的cellForRowAtIndexPath
在课堂上
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(somemethodToReloadtableview) name:@"NOTIFICATIONNAME" object:nil];
}
//Implement the method
-(void)somemethodToReloadtableview{
[self.customTableViewName reloadData];
}
Bm级
-(void)methodFromWhichYouWantToTrigger{
[[NSNotificationCenter defaultCenter]postNotificationName:@"NOTIFICATIONNAME" object:nil];
}
PS - 此方法将调用 tableview 的所有数据源,例如numberOfRows
。 希望这可以帮助
我不确定您为什么要手动调用tableView: cellForRowAtIndexPath:
。 如果你想让你的表视图重新加载数据,你可以在你的表视图上调用reloadData
方法。
[yourTableView reloadData];
如果你想从另一个视图控制器触发它,你可以从另一个视图控制器发布一个通知,并从表视图所在的那个视图控制器中收听它。 Saheb 的回答描述了如何发布通知并听取通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.