I’m trying to create autolayout and autoresize collection view within a uitableview.
so I have a picture and below it is text, which can be one line or more.
But if they are in the same row, I want to get the same height.
Currently, they’re somewhat working, but the height can be different for each cell in the same row.
Also, I’m getting constraint issues, but I think I setup the constraint correctly.
//
// ViewController.m
// collectionview
//
// Created by Richard Lung on 5/17/16.
// Copyright © 2016 Richard Lung. All rights reserved.
//
#import "ViewController.h"
#import "PureLayout.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.edgesForExtendedLayout = UIRectEdgeNone;
[self.view addSubview:self.tableView];
[self.tableView autoPinEdgesToSuperviewEdges];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.estimatedRowHeight = 300;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if(!cell) {
cell = [[CollectionViewTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
}
[cell configureCell];
return cell;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableView *) tableView {
if(_tableView) return _tableView;
_tableView = [[UITableView alloc] initForAutoLayout];
return _tableView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@implementation CollectionViewTableCell
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self) {
}
[self.contentView addSubview:self.collectionView];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView autoPinEdgesToSuperviewEdges];
return self;
}
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AutoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if(indexPath.row == 0) {
cell.actionLabel.text = @"test";
}
else if (indexPath.row == 1) {
cell.actionLabel.text = @"testtesttesttesttesttest";
}
else {
cell.actionLabel.text = @"abcabcabcabcabc";
}
return cell;
}
- (UICollectionView *) collectionView {
if(_collectionView) return _collectionView;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
flowLayout.estimatedItemSize = CGSizeMake(1, 1);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
[_collectionView registerClass:[AutoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[_collectionView setScrollEnabled:NO];
return _collectionView;
}
- (CGSize) systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority {
//self.collectionView.frame = CGRectMake(0, 0, targetSize.width, MAXFLOAT);
[self.collectionView layoutIfNeeded];
// If the cell's size has to be exactly the content
// Size of the collection View, just return the
// collectionViewLayout's collectionViewContentSize.
return [self.collectionView.collectionViewLayout collectionViewContentSize];
}
- (void) configureCell {
//[self.contentView layoutIfNeeded];
[self.collectionView layoutIfNeeded];
}
@end
@implementation AutoCollectionViewCell
- (instancetype) init {
self = [super init];
if (self) {
self.contentView.backgroundColor = [UIColor redColor];
self.imageView=[[UIImageView alloc]initForAutoLayout];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
self.actionLabel = [[UILabel alloc] initForAutoLayout];
self.actionLabel.textColor = [UIColor blackColor];
self.actionLabel.numberOfLines = 0;
[self.contentView addSubview:self.imageView];
[self.contentView addSubview:self.actionLabel];
[self.imageView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 10, 10, 10) excludingEdge:ALEdgeBottom];
// [self.imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10 relation:NSLayoutRelationGreaterThanOrEqual];
[self.imageView autoSetDimension:ALDimensionHeight toSize:125];
[self.imageView autoSetDimension:ALDimensionWidth toSize:125 relation:NSLayoutRelationGreaterThanOrEqual];
[self.actionLabel autoSetDimension:ALDimensionWidth toSize:125 relation:NSLayoutRelationLessThanOrEqual];
[self.actionLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.imageView withOffset:10 relation:NSLayoutRelationGreaterThanOrEqual];
[self.actionLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 10, 0, 10) excludingEdge:ALEdgeTop];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.contentView.backgroundColor = [UIColor redColor];
self.imageView=[[UIImageView alloc]initForAutoLayout];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
self.actionLabel = [[UILabel alloc] initForAutoLayout];
self.actionLabel.textColor = [UIColor blackColor];
self.actionLabel.numberOfLines = 0;
[self.contentView addSubview:self.imageView];
[self.contentView addSubview:self.actionLabel];
[self.imageView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 10, 10, 10) excludingEdge:ALEdgeBottom];
// [self.imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10 relation:NSLayoutRelationGreaterThanOrEqual];
[self.imageView autoSetDimension:ALDimensionHeight toSize:125];
[self.imageView autoSetDimension:ALDimensionWidth toSize:125];
[self.actionLabel autoSetDimension:ALDimensionWidth toSize:125 relation:NSLayoutRelationLessThanOrEqual];
[self.actionLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.imageView withOffset:10 relation:NSLayoutRelationGreaterThanOrEqual];
[self.actionLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 10, 0, 10) excludingEdge:ALEdgeTop];
}
return self;
}
@end