Ios UITableViewCell内的UICollectionView
我注意到,在Ios UITableViewCell内的UICollectionView,ios,objective-c,iphone,uitableview,uicollectionviewcell,Ios,Objective C,Iphone,Uitableview,Uicollectionviewcell,我注意到,在UITableViewCell中使用UICollectionView时,iOS非常吝啬。我试图实现的是,在UITableViewCell中有一组图像(UICollectionView方法)。我试着模仿Facebook的帖子风格,我想,他们有一个UITableView,有自定义单元格等等,但是这里有一个问题 我成功地将UICollectionView放在UITableViewCell中,它工作正常且完美,没有问题,一切都显示良好,可以点击等等。但是,我试图重构我的代码,将UIColle
UITableViewCell
中使用UICollectionView
时,iOS非常吝啬。我试图实现的是,在UITableViewCell
中有一组图像(UICollectionView
方法)。我试着模仿Facebook的帖子风格,我想,他们有一个UITableView
,有自定义单元格等等,但是这里有一个问题
我成功地将UICollectionView
放在UITableViewCell
中,它工作正常且完美,没有问题,一切都显示良好,可以点击等等。但是,我试图重构我的代码,将UICollectionViewDataSource
和UICollectionViewDelegate
提取到单独的文件中,以便重用它们,并且在主视图控制器上拥有更少的代码,当我的噩梦开始时,我不知道如果我使用单独的文件,为什么会改变单元格的配置。例如:
-(void)configureCell:(UICollectionViewCell*)customCell atIndexPath:(NSIndexPath*)indexPath;
上述方法在流程的两个部分调用它:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)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
”方法:
当调试器启动时,方法configureCell:atIndexPath:
会被反复调用。这是第二次发生这种情况。这是否意味着我不能“分离”DataSource
?或在UITableViewCell
中添加UICollectionView
是不可能的。或者,我使用的方法不好吗?有没有办法做到这一点
我对此很困惑
谢谢大家!我为此挣扎了很长时间,花了好几个小时重新设计布局等等。
我终于找到了解决办法:
这是我手机的头文件
@interface MyCustomTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *someLabel;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
-(void)setImages:(NSArray*)images;
@end
这是所有这些之外的魔力——实现文件(m)
在实现文件上设置了它,并在委托方法上调用了MWPhotoBrowser。我知道这真的很奇怪,因为它迫使我使用一个单元格来显示数据源和委托。当我应该能够在其他点回收我的数据源和委托时,这很奇怪。但它起了作用
谢谢你们,伙计们!我遇到了同样的问题..到目前为止还没有找到解决方案。只通过整个单元格配置来确定行高度是不好的。不知道如何计算高度:/n我看到的所有示例都建议这样做
@interface MyCustomTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *someLabel;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
-(void)setImages:(NSArray*)images;
@end
@interface MyCustomTableViewCell() <UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) NSArray *imageArray;
@end
@implementation MyCustomTableViewCell
-(void)setImages:(NSArray*)images{
_imageArray = images;
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource Methods
...
...
...
#pragma mark - UICollectionViewDelegate Methods
...
...
...
@end
-(void)setViewController:(UIViewController*)viewController;