2013-07-29 44 views
0

我已经建造这样的方法:如何直接调用tableView方法?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

//some table related stuff 

} 

但是我不能叫这个,所以我基本上复制和粘贴的整体功能,并更名为:

- (void)jumpCountry: (NSIndexPath *)indexPath { 
//some table related stuff 
} 

,并通过调用此方法:

[self jumpCountry:countryIndex];

但是我的类看起来难看(而不是首选),因为它已经拿到了SAM两种方法。那么,我怎样才能直接调用初始方法(我知道它被分配给一个调用该方法的按钮)。我正在使用iOS6.1。基本上,我想直接调用的原因是我有另一个线程侦听通知(来自位置服务),一旦收到通知,应该更改表视图。通知本身已经在搜索NSIndexPath,所以不会有任何问题。

+0

你为什么不能叫'的tableView:didSelectRowAtIndexPath方法:'? – Wain

+0

编译器提供了一个错误“使用未声明的标识符didSelectRowAtIndexPath'” –

+0

我不明白你的问题,你能说清楚吗?为什么不能从'didSelectRowAtIndexPath'调用'[self jumpCountry:countryIndex];',而不是重复代码? –

回答

1

你可以把

[self jumpCountry:countryIndex]; 

给你的方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
+0

这不是我想要做的。我想从类的内部或外部调用didSelectRowAtIndexPath方法 –

+0

? –

+0

里面的类 –

0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

这种方法从UITableViewDelegate协议表视图的委托方法和它得到调用时,用户在选择一行UITableView。你不应该由你自己来调用这个方法。

而不是你可以创建你的方法,做任何你想做的事情,并在viewDidLoad或任何方法调用它。

+0

我知道我不应该这样称呼它,但我怎么称呼它呢? –

+0

你想在didSelectRowAtIndexPath方法中做什么可以请你解释一下@SarpKaya? – Suryakant

+0

只要你清楚自己在做什么以及为什么(并且添加评论,以便人们知道未来),就没有一条规则说你不应该这么称呼它。 – Wain

0

要调用编程使用

[tabelView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

这里scrollIndexPath是行的indexpath来选择

为了创建indexpath

NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
+0

@sarp Kaya:这会试试这个 –

相关问题