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 个表视图?
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
