2014-09-23 47 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 &其他地方的代表,很奇怪。但它的工作!

謝謝,夥計們!

相關問題