【问题标题】:how to use CollectionView with TableView如何将 CollectionView 与 TableView 一起使用
【发布时间】:2014-10-24 04:18:53
【问题描述】:

您好,我们如何将 Collection View 与 iOS 7+ 的 Table View 连接到 iPhone 应用程序..

我想在屏幕的顶部有一个滚动视图,在剩下的屏幕上,我想有一个 TableView,基本上显示有关用户在 CollectionView 中选择的任何内容的更多信息。

由于我是 iOS 新手,如果您能指出任何有用的资源,那就太好了。

干杯

【问题讨论】:

    标签: ios uitableview uicollectionview


    【解决方案1】:

    有很多方法可以做到这一点,但我通常在一个视图控制器中同时进行。有些人会不同意这种方法。不过比较简单,简单就好。

    为此,您可以创建一个空白 VC 并初始化您的集合视图和表格视图并添加到 VC 的视图中,并将您的 VC 指定为委托和数据源。然后,您实现集合视图和表视图方法和委托方法。选择 CollectionView 单元格后,您可以更新表视图数据源(如果是数组)或针对您的核心数据运行谓词,然后重新加载表视图以获取新的详细信息。

    请确保为 UICollectionView 和 UITableView 实现所有必需的方法,这些方法可以从 Apple 文档中获得。如果你这样做,它应该很简单。

    我不知道你对这个问题投了反对票。

    【讨论】:

      【解决方案2】:

      最后我设法让这个半工作..感谢RegularExpression! (显然,一路上有很多学习)..但是,我注意到..在集合视图上,'cellForItemAtIndexPath'被调用了两次..结果..我的表格视图总是显示下一个单元格的数据..无法弄清楚发生了什么..这是我的 VC 代码..我无法锻炼我做错了什么..我是否需要考虑在主视图上使用 2 个容器并添加 CollectionViewController 和 TableViewController..not确定那是更好的方法..但在我采用这种方法之前..想看看是否有办法解决我现在所拥有的..

      - (void) setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
      
          _managedObjectContext = managedObjectContext;
      
          [self performFetchForCV];
      
      }
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          UICollectionViewFlowLayout *flowLayout = [[LineLayout alloc] init];
          [self.collectionView setCollectionViewLayout:flowLayout];
      }
      
      -(void)performFetchForCV {
      
          if (!_fetchedResultsController) {
              NSFetchRequest *request= [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
              gymClassesRequest.predicate = nil;
              gymClassesRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                                  ascending:YES
                                                                                   selector:@selector(localizedStandardCompare:)]];
      
              _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
          }
      
          NSError *error = nil;
          if (![_fetchedResultsController performFetch:&gymClassesError]) {
              NSLog(@"Unresolved error %@, %@", gymClassesError, [error userInfo]);
              abort();
          }
      
          [self.collectionView reloadData];
      }
      
      - (void)performFetchForTV:(NSPredicate *)predicate {
      
          if (!_fetchedResultsControllerForTV) {
              NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity2"];
              gymScheduleRequest.predicate = nil;
              gymScheduleRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"day" ascending:YES selector:@selector(localizedStandardCompare:)]];
      
              _fetchedResultsControllerForTV = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"day" cacheName:nil];
          }
      
          [_fetchedResultsControllerForTV.fetchRequest setPredicate:predicate];
      
          NSError *error = nil;
          if (![_fetchedResultsControllerForTV performFetch:&error]) {
              NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
              abort();
          }
      
          [self.tableView reloadData];
      
      }
      
      - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
      
          MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseCollectionViewCellIdentifier forIndexPath:indexPath];
      
          MyEntity *entity= [self.fetchedResultsController objectAtIndexPath:indexPath];
      
          cell.nameLabel.numberOfLines = 0;
          cell.nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
          cell.nameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
          cell.nameLabel.text = entity.name;
      
          NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name= %@", entity.name];
          [self performFetchForTV:predicate];
      
          return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-10-19
        • 2018-03-30
        • 2022-01-07
        • 1970-01-01
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 2017-04-20
        • 1970-01-01
        相关资源
        最近更新 更多