cell自適應高度

#import "QHArbCollectionViewCell.h"

@interface QHArbFourTableViewCell()<UICollectionViewDataSource, UICollectionViewDelegate>

@end
@implementation QHArbFourTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
  
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    self.myCollectionView.scrollEnabled = NO;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView registerNib:[UINib nibWithNibName:@"QHArbCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"QHArbCollectionViewCell"];
    // Initialization code
}

+ (NSString *)identifier {
    return @"QHArbFourTableViewCell";
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)clickBtn:(void (^)( ))btnBlock {
    _btnBlock = btnBlock;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count+1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    QHArbCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QHArbCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row == _dataArray.count) {
        cell.image = [UIImage imageNamed:@"friend_add_Groupadd"];
    }else{
        cell.image = _dataArray[indexPath.row];
    }
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((Width-20)/4, (Width-20)/4);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == _dataArray.count) {
        if (_btnBlock) {
            _btnBlock();
        }
        
    }
    
}
- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    
    [self.myCollectionView reloadData];
}



- (CGFloat )cellHeight
{
    if ((_dataArray.count +1)%4 ==0) {
        return 82 +(_dataArray.count +1)/4*(Width-20)/4;
    }
    return 82 + ((_dataArray.count +1)/4+1)*(Width-20)/4;
}
@end
#import "QHP2PArbitrateModel.h"

@interface QHArbFourTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *imageTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (copy, nonatomic) NSArray *dataArray;
@property (copy, nonatomic) void(^btnBlock)();
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *infoLabelHeight;

@property (nonatomic, strong) QHP2PArbitrateModel *model;
+ (NSString *)identifier;

- (void)clickBtn:(void(^)())btnBlock;

- (CGFloat )cellHeight;

@end

在VC.m中調用ide

QHArbFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[QHArbFourTableViewCell identifier]];
cell.dataArray = _images;
return [cell cellHeight];;

效果圖以下圖所示:atom

相關文章
相關標籤/搜索