標準MVC下生成動態生成UICollectionViewCell的size(Controller內完成)

上篇文章說明了標準MVC下如何動態生成標準MVC下生成動態生成UICollectionViewCell的size,并且在此部分工作由View完成。連接如下:

標準MVC下生成動態生成UICollectionViewCell的size(View內完成)

這篇文章主要說明的是如何在Controller實現這部分功能。
效果圖如下(跟之前沒什么區別)

屏幕快照 2016-05-25 下午5.02.54.png

屏幕快照 2016-05-25 下午5.03.11.png

同樣自定義UICollectionViewCell:

#import <UIKit/UIKit.h>

@interface LCHCollectionViewCell : UICollectionViewCell

@property (nonatomic, strong) UILabel *firstLabel;

@property (nonatomic, strong) UILabel *secondLabel;

@end

然后重寫其initWithFrame方法,用于將label加到其contentView中去:

@implementation LCHCollectionViewCell

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _firstLabel = [[UILabel alloc] init];
        _firstLabel.numberOfLines = 1;
        _firstLabel.textAlignment = NSTextAlignmentCenter;
        _firstLabel.backgroundColor = [UIColor redColor];
        _firstLabel.font = [UIFont systemFontOfSize:25];
        
        _secondLabel = [[UILabel alloc] init];
        _secondLabel.numberOfLines = 1;
        _secondLabel.textAlignment = NSTextAlignmentCenter;
        _secondLabel.backgroundColor = [UIColor greenColor];
        _secondLabel.font = [UIFont systemFontOfSize:25];
        
        [self.contentView addSubview:_firstLabel];
        [self.contentView addSubview:_secondLabel];
        
        self.backgroundColor = [UIColor blackColor];
    }
    
    return self;
}

@end

label簡單的alloc init之后加到contentView中即可,不用向我這樣設置這些屬性,畢竟這種方式下C需要知道V的任何細節,所以這些屬性應該由C來完成。

接下來在UICollectionViewDelegateFlowLayout協議中

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

的這個方法內,計算并返回當前M數據中所需的V的size (V就是一個容器,M是內容,所以知道內容之后就應該知道容器的大小)。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LCHModel *model = self.models[indexPath.row];
    
    CGSize sizeForFirstLabel = [model.firstString boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, __FLT_MAX__) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:25]} context:nil].size;
    
    CGSize sizeForSecondLabel = [model.secondString boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, __FLT_MAX__) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:25]} context:nil].size;

    return CGSizeMake(sizeForFirstLabel.width + sizeForSecondLabel.width, MAX(sizeForFirstLabel.height, sizeForSecondLabel.height));
    
}

然后在UICollectionViewDataSource的

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

這個方法中,詳細設置V的subviews的frame

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LCHCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    LCHModel *model = self.models[indexPath.row];
    cell.firstLabel.text = model.firstString;
    cell.secondLabel.text = model.secondString;
    
    CGSize sizeForFirstLabel = [model.firstString boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, __FLT_MAX__) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:25]} context:nil].size;
    
    CGSize sizeForSecondLabel = [model.secondString boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, __FLT_MAX__) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:25]} context:nil].size;
    
    CGRect firstLabelFrame = cell.firstLabel.frame;
    CGRect secondLabelFrame = cell.secondLabel.frame;
    firstLabelFrame.size = sizeForFirstLabel;
    secondLabelFrame.size = sizeForSecondLabel;
    secondLabelFrame.origin.x = sizeForFirstLabel.width;
    cell.firstLabel.frame = firstLabelFrame;
    cell.secondLabel.frame = secondLabelFrame;
    
    return cell;
}

這個Demo的地址如下:

標準MVC下生成動態生成UICollectionViewCell的size(View內完成)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容