【发布时间】:2023-03-27 23:10:02
【问题描述】:
是否可以有两个 UITableViews 使用同一个控制器文件?如果是这样,您如何区分两者?如果没有,你将如何控制它们?
【问题讨论】:
标签: iphone objective-c ipad uitableview
是否可以有两个 UITableViews 使用同一个控制器文件?如果是这样,您如何区分两者?如果没有,你将如何控制它们?
【问题讨论】:
标签: iphone objective-c ipad uitableview
肯定是的。为每个表视图创建两个实例变量。
表视图委托方法包括一个指向表视图的指针,在方法中调用它们,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView != [self tableView1]){
//do stuff
}
//handle tableView2
else{
}
【讨论】:
是的,这是可能的。
由于 UITableViewDelegate 和 UITableViewDataSource 接口的方法签名,差异是可能的。如果您查看任一界面,您会注意到它们都传递了对调用该方法的 UITableView 实例的引用...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
查看UITableViewDelegate 和UITableViewDataSource API 文档了解更多信息。
【讨论】:
是的,正如其他海报所示,这是可能的。
但是还有另一种方法,每个 TableView 都有自己的控制器。这些控制器在另一个 UIViewController 中被实例化。这样耦合度较低,因此重用能力更强。两个 UIViewController 之间的通信使用委托。
Have a look on my sample code.
【讨论】:
为你的 UITableViews 使用不同的标签。
【讨论】: