我有一个带有按钮的项目,允许用户在 ListView (UITableView
) 和 GridView (UICollectionView
) 之间切换,但我不知道如何做吧。
最佳答案
假设您的 Controller 有一个名为 tableView
的 UITableView
属性和一个名为 collectionView
的 UICollectionView
属性。在您的 viewDidLoad
中,您需要添加起始 View 。假设它是 TableView :
- (void)viewDidLoad
{
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
}
然后在您的按钮回调中,交换 View :
- (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];
}
关于ios - 如何切换 UITableView 和 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139371/