Ios 如何切换UITableView和UICollectionView
我有一个带有按钮的项目,允许用户在列表视图(Ios 如何切换UITableView和UICollectionView,ios,uitableview,user-interface,uicollectionview,Ios,Uitableview,User Interface,Uicollectionview,我有一个带有按钮的项目,允许用户在列表视图(UITableView)和网格视图(UICollectionView)之间切换但是我不知道怎么做。让我们假设您的控制器有一个名为tableView的UITableView属性和一个名为collectionView的UICollectionView属性。在viewDidLoad中,需要添加起始视图。假设是表视图: - (void)viewDidLoad { self.tableView.frame = self.view.bounds;
UITableView
)和网格视图(UICollectionView
)之间切换但是我不知道怎么做。让我们假设您的控制器有一个名为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://code>:
- (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];
}
另一种处理方法是使用单个UICollectionView
,您可以根据需要的模式切换UICollectionViewFlowLayout
实现
例如,为了从UITableView
转换到UICollectionView
,网上有很多教程。英语不是很差,但目前这是一个很差的问题。你到底坚持什么?到目前为止你想做什么?对不起@我是个新手!谢谢但是我可以使用哪个ViewController?TableViewController或CollectionViewController。如何在外包类(不是ViewController)中使用TableViewDelegate和Datasource?只需使用普通的UIViewController,并将这两种类型的视图作为属性添加到其中即可。或者,如果要保持两个视图的逻辑独立,可以创建UITableViewController和UICollectionViewController,并使用它们的视图而不是主控制器的属性。如果视图已经是视图层次结构的一部分(例如,通过使用xib/故事板添加)您必须将UIViewAnimationOptions ShowHideTransitionViews
作为+transitionFromView:ToView:duration:options:completion:
方法中的选项参数之一传递。您应该使用UIViewAnimationOptions TransitionFromRight
而不是UIViewAnimationTransitionFromRight
。这太棒了谢谢但是,我如何让子视图的单元格在选中时调用父视图上的方法呢?我想将一个控制器推送到家长的导航控制器上