iPhone:如何在一个NIB中控制两个表 [英] iPhone: How to control two tables within one NIB
问题描述
我需要在同一NIB中使用两个UITableViews.我已经使用IB并使用两个表创建了一个视图.
I need two UITableViews in the same NIB. I have used IB and created a view with the two tables.
我的头文件包含ViewController和两个类,每个表一个(请参见下文).在IB中,我可以将每个表的委托和数据源连接到FileOwner,但是我无法确定如何创建IBOutlet连接.我收到此消息: -[新闻tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x15d3c02011-12-11 07:20:27.480 myCity1 [659:707]由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[新闻tableView:numberOfRowsInSection:]:无法识别的选择器已发送至实例0x15d3c0'
My header file contains the ViewController and two classes, one for each of the tables (see below). In IB I can connect each table's delegate and datasource to FileOwner, but I cannot work out how to create the IBOutlet connection. I'm getting this message: -[News tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x15d3c0 2011-12-11 07:20:27.480 myCity1[659:707] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[News tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x15d3c0'
这是头文件:
@interface News : UIViewController {
}
@end
@interface TownNews : UITableViewController {
UITableView *townNewsTable;
}
@property (nonatomic, retain) IBOutlet UITableView *townNewsTable;
@end
@interface GeneralNews : UITableViewController {
UITableView *generalNewsTable;
}
@property (nonatomic, retain) IBOutlet UITableView *generalNewsTable;
@end
推荐答案
我正在我的一个项目中处理此问题(您可以观看:在App Store中名为IJCAI11的免费应用;"People"标签(索引是通过单独的tableView完成的));就我在上面的代码中看到的而言,您正在使事情变得过于复杂.我将描述我是如何做到的,而不是说你不应该做的事情:)
I was dealing with this thing in one of my projects (you can watch it: free app with name IJCAI11 in the appstore; the tab 'People' (there the indexing is done with a separate tableView)); and as far as I see by the code above, you're making things to be too complicated. I'll describe how I did it instead of saying what you shouldn't do :)
我只有一个类,比方说YaddaYaddaViewController.
I had only one class, let's say YaddaYaddaViewController.
@interface YaddaYaddaViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UITableView *tableView1;
UITableView *tableView2;
}
@property (nonatomic, retain) IBOutlet UITableView * tableView1;
@property (nonatomic, retain) IBOutlet UITableView * tableView2;
@end
请注意,viewController派生自UIViewController,而不是UITableViewController(UITableViewController类的目标是能够在2分钟内实现简单的tableView,对于更多自定义内容,您应始终使用UIViewController.浏览UITableViewController类的文档.
Note that the viewController is derived from UIViewController, not UITableViewController (the goal of class UITableViewController is to be able to implement simple tableView in 2 minutes, for more custom stuff you should always use UIViewController. To read more about that, take a glance at the documentation of UITableViewController class).
在IB的文件所有者"中,我只设置了YaddaYaddaViewController;我将tableView1和tableView2出口链接到View中适当的表;我将委托和数据源都链接到两个tableView1/2上,所以我有多个"委托和数据源.
In the IB for "File's Owner" I simply set YaddaYaddaViewController; I link the tableView1 and tableView2 outlets to the proper tables in the View; I link the delegate and the datasource to the both tableView1/2, so I have 'multiple' delegate and datasource.
最后,在代码中,针对所有委托方法(例如tableView:cellForRowAtIndexPath :)我只是注意tableView的值,该事件的委托事件导致调用此方法,例如:
And finally, in the code, for all delegate methods (e.g. tableView:cellForRowAtIndexPath:) I just pay attention to the tableView value, a delegate-event of which caused the calling of this method, like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.tableView1) {
// some code here
} else {
// alternative code here
}
}
希望这会有所帮助!
这篇关于iPhone:如何在一个NIB中控制两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!