我注意到 iOS 在 UITableViewCell
中使用 UICollectionView
时非常不友好。我想要实现的目标是在 UITableViewCell
中有一个图像集合(UICollectionView
方法)。我想模仿 Facebook 的帖子风格,我想,他们有一个 UITableView
,带有自定义单元格等等,但是这里是问题...
我已经设法将 UICollectionView
放入 UITableViewCell
中,它工作正常且完美,没问题,一切都显示正常,可以点击等等。但是,我试图重构我的代码,将 UICollectionViewDataSource
和 UICollectionViewDelegate
提取到单独的文件中,以便重用它们并拥有Main View Controller
上的代码更少,就在这里,当我的噩梦开始时,我不知道为什么如果我使用单独的文件 iOS 循环配置细胞。例如:
-(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
”方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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
。或者,我使用的方法不好吗?有没有办法做到这一点?
我对此很困惑......
谢谢大家!
最佳答案
我为此苦苦挣扎了很长时间,花了几个小时重新设计布局等等。 我终于得到了解决方案:
这是我的cell的头文件
@interface MyCustomTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *someLabel;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
-(void)setImages:(NSArray*)images;
@end
这就是实现文件之外的魔法 (m)
@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
使用上述方法使 CollectionView 显示并显示其内容。我还想向 CollectionView 添加一个操作,例如,在单击图像时显示 MWPhotoBrowser,我向单元格的 header 文件添加了一个方法:
-(void)setViewController:(UIViewController*)viewController;
在实现文件上设置它并在委托(delegate)方法上调用 MWPhotoBrowser。我知道这很奇怪,因为它迫使我使用 Cell 来显示 DataSource 和 Delegate。当我应该能够在其他点回收我的数据源和委托(delegate)时,很奇怪。但它奏效了!!
谢谢你们,伙计们!
关于ios - UITableViewCell 中的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25999400/