ios - 如何切换 UITableView 和 UICollectionView

标签 ios uitableview user-interface uicollectionview

我有一个带有按钮的项目,允许用户在 ListView (UITableView) 和 GridView (UICollectionView) 之间切换,但我不知道如何做吧。

最佳答案

假设您的 Controller 有一个名为 tableViewUITableView 属性和一个名为 collectionViewUICollectionView 属性。在您的 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/

相关文章:

ios - 通知哪个调用是在与可达性无关的情况下进行的

ios - 用 UIDatePicker 替换 UITextField 输入

ios - 通过表格附件按钮将字符串值传递给不同的 View Controller

ios - 使用 GTM OAuth2 难以通过谷歌存储对 iOS 进行身份验证

ios - 应用程序崩溃,说集合表达式类型UIView *'可能不响应'countByEnumeratingWithState:object:count“'

ios - 在 UITableView 中播放视频

ios - 如何更改 UITableView Swift 3 中的分隔符高度?

java - JSlider 的刻度问题

JavaFX:用线程移动一个圆圈

iPhone UI 库?