2014-09-23 101 views
2

我注意到iOS在UITableViewCell内使用UICollectionView时非常的平均。我试图实现的目标是在UITableViewCell内部收集图像(UICollectionView方法)。我试图模仿Facebook发布的风格,我想,他们有一个UITableView,定制细胞等,但这里的问题...UITollectionView里面的UITableViewCell

我已经成功地把UICollectionViewUITableViewCell内,它的工作正确完美,没有问题,一切都很好,可点击等等。但是,我想修改我的代码,提取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; 
} 

上述方法它调用使用自动版式来计算行高我在这里阅读上一个帖子计算器(不记得交:()

它被计算等等,另一种方法调用“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。或者,我是否使用了一种不好的方法?有没有办法做到这一点?

我对此很困惑......

谢谢各位!

+0

我有同样的问题..到目前为止还没有得到解决方案。 – 2014-09-24 06:31:30

+0

通过整个单元配置来确定行高是不好的。 – 2014-09-24 15:53:34

+0

不知道如何计算身高:/我看到的所有例子都建议这么做 – NemesisDoom 2014-09-24 19:41:04

回答

2

我这个挣扎了很长的时间,花了几个小时重新设计布局,等等,等等 我终于得到了解决方法:

这是我的手机的头文件

@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影像它被点击的时候,我添加了一个方法将细胞的头文件

-(void)setViewController:(UIViewController*)viewController; 

Seted它的实现文件并在委托方法上调用MWPhotoBrowser。我知道这很奇怪,因为它迫使我使用Cell来显示DataSource代理。当我应该能够回收我的DataSource &其他地方的代表,很奇怪。但它的工作!

谢谢,伙计们!

相关问题