微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 如何切换UITableView和UICollectionView

我有一个项目有一个按钮,允许用户在列表视图(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:]改为:

- (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];
}

原文地址:https://www.jb51.cc/iOS/330388.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐