How to manage 2 tableviews in 1 viewController?(如何在 1 个视图控制器中管理 2 个表视图?)
问题描述
有谁知道在一个 viewController 中管理多个 tableView 的简单方法?到目前为止,我是这样做的:
Does anyone know a simple way to manage several tableViews in one viewController? Here is how I've been doing it so far:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == self.tableView1)
return 1;
else if(tableView == self.tableView2)
return 2;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView == self.tableView1)
return @"bla";
else if(tableView == self.tableView2)
return @"blabla";
}
-(NSString *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tableView1)
...
else if(tableView == self.tableView2)
...
}
我发现对于每一个委托方法都必须使用 if/else 语句真的很烦人.另外,当有很多 tableViews 时,真的很难阅读.此外,我在 NSURLConnection 等方面也有同样的问题……只要我有几个对象响应相同的委托协议,事情就会变得一团糟.
I find it really annoying that I have to use an if/else statement for EVERY SINGLE delegate method. Plus, it is really hard to read when there are many tableViews. Besides, I have the same problem with NSURLConnection, etc... As soon as I have several objects that respond to the same delegate protocol, things get messy.
让事情变得更简单的最佳方法是什么?谢谢
What is the best way to make things simpler? Thanks
推荐答案
您可以为表视图使用选择器和某种标识符(例如 UIView 标记).像这样的:
You could use selectors and some kind of identifier for the table views (the UIView tag, for example). Something like this:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self performSelector:NSSelectorFromString([NSString stringWithFormat:@"tableView%d:titleForHeaderInSection:", tableView.tag])];
}
当然,您需要为每个表视图设置一种方法.假设您的两个表有标签 100 和 101,那么您将有 tableView100:titleForHeaderInSection 和 tableView101:titleForHeaderInSection.
Of course you will need to have one method for each of your table views. Suppose your two tables have a the tags 100 and 101, you will have then tableView100:titleForHeaderInSection and tableView101:titleForHeaderInSection.
这篇关于如何在 1 个视图控制器中管理 2 个表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 1 个视图控制器中管理 2 个表视图?
基础教程推荐
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
