ios – 如何切换UITableView和UICollectionView

前端之家收集整理的这篇文章主要介绍了ios – 如何切换UITableView和UICollectionView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个项目有一个按钮,允许用户在列表视图(UITableView)和网格视图(UICollectionView)之间切换.
但我不知道要做什么请帮帮我. (对不起英语不好)

解决方法

假设您的控制器具有名为tableView的UITableView属性和名为collectionView的UICollectionView属性.在您的viewDidLoad中,您需要添加起始视图.我们假设这是表格视图:
  1. - (void)viewDidLoad
  2. {
  3. self.tableView.frame = self.view.bounds;
  4. [self.view addSubview:self.tableView];
  5. }

然后在你的按钮回调中,交换意见:

  1. - (void)buttonTapped:(id)sender
  2. {
  3. UIView *fromView,*toView;
  4.  
  5. if (self.tableView.superview == self.view)
  6. {
  7. fromView = self.tableView;
  8. toView = self.collectionView;
  9. }
  10. else
  11. {
  12. fromView = self.collectionView;
  13. toView = self.tableView;
  14. }
  15.  
  16. [fromView removeFromSuperview];
  17.  
  18. toView.frame = self.view.bounds;
  19. [self.view addSubview:toView];
  20. }

如果你想要一个花哨的动画,你可以使用[UIView transitionFromView:toView:duration:options:completion:]改为:

  1. - (void)buttonTapped:(id)sender
  2. {
  3. UIView *fromView,*toView;
  4.  
  5. if (self.tableView.superview == self.view)
  6. {
  7. fromView = self.tableView;
  8. toView = self.collectionView;
  9. }
  10. else
  11. {
  12. fromView = self.collectionView;
  13. toView = self.tableView;
  14. }
  15.  
  16. toView.frame = self.view.bounds;
  17. [UIView transitionFromView:fromView
  18. toView:toView
  19. duration:0.25
  20. options:UIViewAnimationTransitionFlipFromRight
  21. completion:nil];
  22. }

猜你在找的iOS相关文章