ios 一个 UIView 中有 2 个 UITableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10649412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
2 UITableViews in one UIView
提问by woutr_be
I have a UIView
that will need to display two UITableViews, but they are never shown together, by using a SegementedBar
you can toggle one or the other.
我有一个UIView
需要显示两个 UITableViews,但它们永远不会一起显示,通过使用SegementedBar
你可以切换一个或另一个。
What would be the best way to handle this? Just create one Table View Controller
and change the data source, or create 2 Table View Controllers
and just hide one when the other is visible.
处理这个问题的最佳方法是什么?只需创建一个Table View Controller
并更改数据源,或者创建 2 个Table View Controllers
并在另一个可见时隐藏一个。
The 2 tables will have a completely different layout with different custom cells.
这 2 个表格将具有完全不同的布局和不同的自定义单元格。
回答by Damo
I would keep one datasource & delegate.
我会保留一个数据源和委托。
This means that all the delegate/datasource methods become more complicated BUT it means that you can retain the one to one relationship between viewController & view.
这意味着所有委托/数据源方法都变得更加复杂,但这意味着您可以保留 viewController 和 view 之间的一对一关系。
keep a reference to each of the table views
保留对每个表视图的引用
//vc.h
@property (nonatomic, weak) IBOutlet UITableView* firstTableView;
@property (nonatomic, weak) IBOutlet UITableView* secondTableView;
In the datasource/ delegate methods you need to account for the fact that the method needs to behave differently depending on which table view is in use. e.g.
在数据源/委托方法中,您需要考虑这样一个事实,即该方法需要根据使用的表视图而表现不同。例如
//vc.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView == self.firstTableView) {
...
} else { // tableView == self.secondTableView
...
}
}
return cell;
}
}
回答by Warif Akhand Rishi
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView.tag == 1) {
...
} else { // tableView == self.secondTableView
...
}
}
tag could be assigned from the .xib. so no need to have UITableVeiw variable in .h file. Two table view in .xib needed
标签可以从 .xib 中分配。所以不需要在 .h 文件中有 UITableVeiw 变量。需要 .xib 中的两个表格视图
回答by Hemang
In my current app, I need to have 4 UITableView
in a single UIViewController
, at once I've to show single table, based on the tab selected by the user, I've added four tables because, all of having different custom cells and functionality, to reduce complexity I took four.
在我当前的应用程序中,我需要UITableView
在一个单独的4UIViewController
个表中显示单个表,根据用户选择的选项卡,我添加了四个表,因为所有表都具有不同的自定义单元格和功能,为了降低复杂性,我花了四个。
The main benefit of this is that, each time you don't need to call reloadData
to update a single table. I just need to properly handle table's show & hide flow. And believe me that's looks cool. Not flicking at all.
这样做的主要好处是,每次都不需要调用reloadData
更新单个表。我只需要正确处理表格的显示和隐藏流程。相信我,这看起来很酷。一点都不闪。
In my case, I am creating four tables by code only. And I make a method that will return me a table based upon a tag I've pass.
就我而言,我仅通过代码创建四个表。我做了一个方法,它会根据我传递的标签返回一个表。
I keep cellForRowAtIndexPath
as small as possible by dividing code into different functions.
我cellForRowAtIndexPath
通过将代码划分为不同的功能来保持尽可能小。
回答by rishi
Both approach has some pros and cons, but i will personally prefer approach having two separate controller.
这两种方法都有一些优点和缺点,但我个人更喜欢有两个独立控制器的方法。
Approach 1 - create one Table View Controller and change the data source
方法一 - 创建一个 Table View Controller 并更改数据源
- This approach help in avoiding extra and repeated code.
- With this memory management is good as using one controller only.(Although this is not a big concern till then we won't have a lot of data.)
- Issue with this is having complexity.
- 这种方法有助于避免额外和重复的代码。
- 使用这种内存管理就像只使用一个控制器一样好。(尽管在此之前这不是一个大问题,我们不会有很多数据。)
- 问题是复杂性。
Approach 2 - 2 Table View Controller
方法 2 - 2 表视图控制器
- With this approach definitely have extra and repeated code.
- But with this is less complexity.
- 使用这种方法肯定会有额外和重复的代码。
- 但是这样复杂性降低了。
回答by Andreas Ley
Use separate UITableViewControllers
and swap the views. It's less code, less complexity and it's the way Apple does it with the TabBar.
使用单独UITableViewControllers
并交换视图。它的代码更少,复杂性更低,这就是 Apple 使用TabBar 的方式。
As for code complexity, there really isn't any. You simply do the following to switch views when the UISegmentedControl
's value has changed:
至于代码复杂性,真的没有。当UISegmentedControl
的值发生变化时,您只需执行以下操作即可切换视图:
UIView *previousSuperview = myViewController1.view.superview;
myViewController2.view.frame = myViewController1.view.frame;
[myViewController1.view removeFromSuperview];
[previousSuperview addSubview:myViewController2.view];
Alternatively, you could set the corresponding view's hidden
property.
或者,您可以设置相应视图的hidden
属性。