• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableViewCell 中的 UICollectionView

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:52:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我注意到 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap