【问题标题】:tableView in collectionView IOScollectionView IOS中的tableView
【发布时间】:2014-12-18 10:37:24
【问题描述】:

我在 MainStoryboard.storyboard 中使用集合视图,显示图像网格。我想在选择项目时从集合视图转到表格视图,即使用以下方法:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

选择后如何在collectionView的方法中加载tableView?

我的做法:

UIViewController *vc = [[UIStoryboard storyboardWithName:@"SubCategories" bundle:nil] instantiateViewControllerWithIdentifier:@"SubCategories"];
 [self.view  addSubview:vc.view ];

【问题讨论】:

  • 您应该对新场景执行转场,而不是将视图添加到当前视图
  • 请详细解释一下如何在collectionview的方法中执行? @Paulw11
  • 搜索使用方法performSegueWithIdentifier
  • 在 iOS 上执行该操作是一件非常基本的事情。尝试深入研究文档或查看本教程。 raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1。祝你好运!

标签: ios xcode uitableview uicollectionview


【解决方案1】:

您应该在 Storyboard 中创建一个包含 TableView 的新 ViewController。然后将第一个 ViewController(来自 VC 本身,而不是 CollectionView)的 segue 添加到新的 ViewController。确保为该转场设置转场标识符。

然后在你的 didSelectItemAtIndexPath 方法中,调用

[self performSegueWithIdentifier:@"YourIdentifier" sender:self];

然后您可以在您的第一个 VC 中实现 prepareForSegue 方法,以将有关所选内容的数据传递给您的第二个 VC。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Make sure your are about to perform the same segue
    if ([[segue identifier] isEqualToString:@"YourIdentifier"]) {
        // Get reference to the destination view controller
        SecondViewController *vc = [segue destinationViewController];
        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

注意,永远不要调用 prepareForSegue,iOS 会在 segue 即将发生时为你调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-13
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    相关资源
    最近更新 更多