OGeek|极客世界-中国程序员成长平台

标题: ios - UITableViewCell 中的 UICollectionView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:52
标题: ios - UITableViewCell 中的 UICollectionView

我注意到 iOS 在 UITableViewCell 中使用 UICollectionView 时非常不礼貌。我想要实现的是,在 UITableViewCell 中有一个图像集合(UICollectionView 方法)。我想模仿 Facebook 的帖子风格,我想,他们有一个 UITableView,带有自定义单元格等等,但是 这里是问题...

我已设法将 UICollectionView 放入 UITableViewCell 中,它工作正常且完美,没问题,一切正常,可点击等等。但是,我尝试重构我的代码,将 UICollectionViewDataSourceUICollectionViewDelegate 提取到单独的文件中,以便重用它们并拥有Main View Controller 上的代码较少,就在这里,当我的 Nightmare 开始时,我不知道为什么如果我使用单独的文件 iOS Cycles 的配置细胞。例如:

-(void)configureCellUICollectionViewCell*)customCell atIndexPathNSIndexPath*)indexPath;

上面的方法在进程的两个部分被调用:

-(CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath{
 NSString *identifier = [self retrieveIdentifierForIndexPath:indexPath];
 UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:identifier];
 [self configureCell:myCell atIndexPath:indexPath];
 [cell setNeedsLayout];
 [cell layoutIfNeeded];
 [cell setNeedsUpdateConstraints];
 [cell updateConstraintsIfNeeded];
 CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize].height;
 return height;
}

上面的方法被调用来使用 AutoLayout 计算行高我在 StackOverFlow 的帖子上读到了这个(不记得帖子)

在计算高度等之后,另一个方法调用“configureCell”方法:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    NSString *rowName = [self retrieveIdentifierForIndexPath:indexPath];
    PostDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rowName forIndexPath:indexPath];

    //PreCondition
    if([rowName isEqualToString"LoadingRow"]){
        return cell;
    }

    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath withImages:YES];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}

当调试器启动时,configureCell:atIndexPath:方法被一遍又一遍地调用。这是第二次发生这种情况。这是否意味着我不能“分离”DataSource?或者不可能UITableViewCell 中添加 UICollectionView。或者,我是否使用了不好的方法?有没有办法做到这一点?

我对此感到非常困惑......

谢谢你们!



Best Answer-推荐答案


我为此苦苦挣扎了很长时间,花了几个小时重新设计布局等。 我终于得到了解决方案:

这是我的单元格的头文件

@interface MyCustomTableViewCell : UITableViewCell
 @property (strong, nonatomic) IBOutlet UILabel *someLabel;
 @property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

 -(void)setImagesNSArray*)images;
@end

这就是实现文件(m)之外的魔法

@interface MyCustomTableViewCell() <UICollectionViewDataSource, UICollectionViewDelegate>
 @property (strong, nonatomic) NSArray *imageArray;
@end
@implementation MyCustomTableViewCell
-(void)setImagesNSArray*)images{
 _imageArray = images;
 [_collectionView setDataSource:self];
 [_collectionView setDelegate:self];
 [_collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource Methods
...
...
...
#pragma mark - UICollectionViewDelegate Methods
...
...
...
@end

使用上述方法使 CollectionView 显示并显示其内容。我还想在 CollectionView 中添加一个 Action ,例如,当单击图像时显示 MWPhotoBrowser,我在单元格的 Header 文件中添加了一个方法:

-(void)setViewControllerUIViewController*)viewController;

在实现文件上设置它并在委托(delegate)方法上调用 MWPhotoBrowser。我知道这很奇怪,因为它迫使我使用 Cell 来显示 DataSource 和 Delegate。当我应该能够在其他点回收我的 DataSource 和 Delegate 时,很奇怪。但它奏效了!!

谢谢你们!

关于ios - UITableViewCell 中的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25999400/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4