我有一个项目,它有一个按钮,允许用户在列表视图(UITableView)和网格视图(UICollectionView)之间切换,但我不知道怎么做。
发布于 2013-01-03 21:10:05
假设您的控制器有一个名为tableView的UITableView属性和一个名为collectionView的UICollectionView属性。在viewDidLoad中,您需要添加起始视图。让我们假设它是表格视图:
- (void)viewDidLoad
{
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
}然后在你的按钮回调中,换出视图:
- (void)buttonTapped:(id)sender
{
UIView *fromView, *toView;
if (self.tableView.superview == self.view)
{
fromView = self.tableView;
toView = self.collectionView;
}
else
{
fromView = self.collectionView;
toView = self.tableView;
}
[fromView removeFromSuperview];
toView.frame = self.view.bounds;
[self.view addSubview:toView];
}如果你想要花哨的动画,你可以改用+[UIView transitionFromView:toView:duration:options:completion:]:
- (void)buttonTapped:(id)sender
{
UIView *fromView, *toView;
if (self.tableView.superview == self.view)
{
fromView = self.tableView;
toView = self.collectionView;
}
else
{
fromView = self.collectionView;
toView = self.tableView;
}
toView.frame = self.view.bounds;
[UIView transitionFromView:fromView
toView:toView
duration:0.25
options:UIViewAnimationTransitionFlipFromRight
completion:nil];
}发布于 2017-02-14 20:37:27
处理这个问题的另一种方法是使用单个UICollectionView,您可以根据所需的模式切换UICollectionViewFlowLayout实现。
为了从UITableView转换到UICollectionView,有很多在线教程,例如this。
https://stackoverflow.com/questions/14139371
复制相似问题