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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...