繁体   English   中英

如何在TableView中使用CollectionView

[英]how to use CollectionView with TableView

嗨,如何为iPhone应用程序将Collection View与Table View iOS 7+连接。

我想在屏幕顶部上滚动查看,在其余屏幕上,我想使用TableView,从本质上显示有关CollectionView中选择的用户的更多信息。

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

干杯

有多种方法可以完成此操作,但我通常都在单个视图控制器中完成这两种操作。 有些人会不同意这种方法。 但是,它更简单,简单就是好。

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

只需确保为Apple文档中的UICollectionView和UITableView实现所有必需的方法即可。 如果这样做,应该很简单。

我不知道您对这个问题持否定态度。

终于我设法使这个半工作..谢谢RegularExpression! (显然,在此过程中有很多学习内容)。但是,我注意到..在集合视图中,“ cellForItemAtIndexPath”被调用了两次。作为结果。.我的表视图始终显示下一个单元格的数据。无法知道发生了什么。这是我的VC代码。无法执行我做错的事情。我需要研究在主视图上使用2个容器并添加CollectionViewController和TableViewController吗。当然,这是更好的方法。但是在我开始使用该方法之前,我想看看是否有办法可以修复我现在所拥有的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM