What I'm trying to achieve is a view controller that can display the items in both list form and collection form with a button that can switch over between two display options.
我想要实现的是一个视图控制器,它可以显示列表形式和集合形式的项目,并带有一个可以在两个显示选项之间切换的按钮。
So what I've implemented is a UIViewController
that can hold both UITableView
and UICollectionView
.
所以我实现的是一个可以同时包含UITableView和UICollectionView的UIViewController。
When this view controller loads at the first time, the UITableView
appears fine. But when I press the switching button (it simply changes self.view
to UICollectionView
), it ends up with just a black view.
当此视图控制器第一次加载时,UITableView显示正常。但是当我按下切换按钮(它只是将self.view更改为UICollectionView)时,它最终只有一个黑色视图。
and when I try to get back to the UITableView
by pressing switching button (again, it just changes self.view
to previous UITableView
), nothing happens. It just stays black.
当我尝试通过按切换按钮返回到UITableView时(再次,它只是将self.view更改为之前的UITableView),没有任何反应。它只是保持黑色。
Is there something that I should do after changing the root view of view controller?
更改视图控制器的根视图后,我应该做些什么?
3 个解决方案
#1
0
It is not recommended behavior to change the view of a UIViewController
past the point in time at which loadView
is called. You'll want your view controller to have one, persistent view for its lifetime. I'd recommend one of the following.
建议不要在调用loadView的时间点之后更改UIViewController的视图。您希望视图控制器在其生命周期内拥有一个持久视图。我推荐以下之一。
- Layering the table view / collection view on top of your view controller's view. That way you're not monkeying with the main underlying view itself.
- Having two instances of your view controller, one with a table and the other with a collection view, and swapping between those.
在视图控制器的视图顶部分层表视图/集合视图。这样你就不会对主要的底层视图本身进行修改。
有两个视图控制器实例,一个带有表,另一个带有集合视图,并在这些实例之间进行交换。
#2
0
If you are only switching the subviews
of self.view
object.
如果您只是切换self.view对象的子视图。
var tableView:UITableView?
var collectionView:UICollectionView?
var bounds = CGRectMake(0, 0, 320, 480) // frame of tableView
func createTableView() -> UITableView{
if(tableView == nil){
tableView = UITableView(frame: bounds, style: .Plain)
}
tableView?.delegate = self
tableView?.dataSource = self
return tableView;
}
func createCollectionView() -> UICollectionView{
if(collectionView == nil){
collectionView = UICollectionView(frame: bounds, collectionViewLayout: CustomFlowLayout)
}
collectionView?.delegate = self
collectionView?.dataSource = self
return collectionView
}
func initializeViews{
createTableView()
createCollectionView()
self.view.addSubView(tableView);
self.view.addSubView(collectionView);
}
func showTableView{
collectionView?.hidden = true;
tableView?.hidden = false;
}
func showCollectionView{
tableView?.hidden = true;
collectionView?.hidden = false;
}
#3
0
Nevermind! I've tried this and got what I wanted! :) Simply removed from superview and then added back.
没关系!我试过这个,得到了我想要的东西! :)只需从superview中删除然后再添加回来。
UIView *superview = self.view.superview;
[self.view removeFromSuperview];
self.view = self.collectionView;
[superview addSubview:self.view];
#1
0
It is not recommended behavior to change the view of a UIViewController
past the point in time at which loadView
is called. You'll want your view controller to have one, persistent view for its lifetime. I'd recommend one of the following.
建议不要在调用loadView的时间点之后更改UIViewController的视图。您希望视图控制器在其生命周期内拥有一个持久视图。我推荐以下之一。
- Layering the table view / collection view on top of your view controller's view. That way you're not monkeying with the main underlying view itself.
- Having two instances of your view controller, one with a table and the other with a collection view, and swapping between those.
在视图控制器的视图顶部分层表视图/集合视图。这样你就不会对主要的底层视图本身进行修改。
有两个视图控制器实例,一个带有表,另一个带有集合视图,并在这些实例之间进行交换。
#2
0
If you are only switching the subviews
of self.view
object.
如果您只是切换self.view对象的子视图。
var tableView:UITableView?
var collectionView:UICollectionView?
var bounds = CGRectMake(0, 0, 320, 480) // frame of tableView
func createTableView() -> UITableView{
if(tableView == nil){
tableView = UITableView(frame: bounds, style: .Plain)
}
tableView?.delegate = self
tableView?.dataSource = self
return tableView;
}
func createCollectionView() -> UICollectionView{
if(collectionView == nil){
collectionView = UICollectionView(frame: bounds, collectionViewLayout: CustomFlowLayout)
}
collectionView?.delegate = self
collectionView?.dataSource = self
return collectionView
}
func initializeViews{
createTableView()
createCollectionView()
self.view.addSubView(tableView);
self.view.addSubView(collectionView);
}
func showTableView{
collectionView?.hidden = true;
tableView?.hidden = false;
}
func showCollectionView{
tableView?.hidden = true;
collectionView?.hidden = false;
}
#3
0
Nevermind! I've tried this and got what I wanted! :) Simply removed from superview and then added back.
没关系!我试过这个,得到了我想要的东西! :)只需从superview中删除然后再添加回来。
UIView *superview = self.view.superview;
[self.view removeFromSuperview];
self.view = self.collectionView;
[superview addSubview:self.view];