我有一个带有按钮的项目,该按钮允许用户在列表视图( UITableView
)和网格视图( UICollectionView
)之间切换,但是我不知道该怎么做。
Let's suppose that your controller has a UITableView
property named tableView
and a UICollectionView
property named collectionView
. In your viewDidLoad
you need to add the starting view. Let's suppose it's the table view:
- (void)viewDidLoad
{
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
}
Then in your button callback, swap the views out:
- (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];
}
If you want a fancy animation, you can use +[UIView transitionFromView:toView:duration:options:completion:]
instead:
- (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];
}
Another way to deal with it is having a single UICollectionView
in which you can switch the UICollectionViewFlowLayout
implementation depending on the mode you want.
In order to convert from UITableView
to UICollectionView
, there are a lot of tutorials online, for example this .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.