I have an UITableView
with an UICollectionView
in every cell. My problem is that the height of UITableView
never adopted the height of the content of the UICollectionView
. When I change the UICollectionView
to a Label the UITabelViewCell
adopted the height correctly based on the height of the Label text. I tried a lot of different things but nothing worked and I don't want to calculate the height for every cell in heightForRowAtIndexPath
also because I don't know the content in that moment and because I need it really generics because of a wide set of different cells. What I have in the moment is:
NewsFeedViewController
@interface NewsFeedViewController ()
@property (weak, nonatomic) IBOutlet NewsFeedTableView * newsFeedTableView;
@end
@implementation NewsFeedViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.newsFeedTableView.rowHeight = UITableViewAutomaticDimension;
self.newsFeedTableView.estimatedRowHeight = 244.0;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NewsFeedTableView
@interface NewsFeedTableView () <UITableViewDataSource, UITableViewDelegate>
@end
@implementation NewsFeedTableView
#pragma mark - Init
-(void) commonInit
{
self.delegate = self;
self.dataSource = self;
[self registerNib:[UINib nibWithNibName:@"NewsFeedPostTableViewCell" bundle:nil] forCellReuseIdentifier:@"NewsFeedPostTableViewCell"];
}
- (id)initWithCoder:(NSCoder*)aDecoder
{
if(self = [super initWithCoder:aDecoder]) {
[self commonInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]) != nil)
{
[self commonInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
if ((self = [super initWithFrame:frame style:style]) != nil)
{
[self commonInit];
}
return self;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NewsFeedTableViewCell *cell;
cell = (NewsFeedTableViewCell *)[self configCellAtIndexPath:indexPath forTableView:tableView];
return cell;
}
- (UITableViewCell *)configCellAtIndexPath:(NSIndexPath *)indexPath
forTableView:(UITableView *)tableView
{
NewsFeedTableViewCell *cell;
static NSString * cellNewCrewId = @"NewsFeedPostTableViewCell";
cell = [self dequeueReusableCellWithIdentifier:cellNewCrewId];
return cell;
}
@end
NewsFeedPostTableViewCell
@implementation NewsFeedPostTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
MemberListCollectionView
@implementation MemberListCollectionView
#pragma mark - LifeCycle
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
[self initLayout];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self initLayout];
}
return self;
}
- (void)initLayout
{
self.delegate = self;
self.dataSource = self;
[self registerClass:[MemberListCollectionViewCell class] forCellWithReuseIdentifier:@"MemberListCollectionViewCell"];
}
- (void)awakeFromNib
{
[self initLayout];
}
#pragma mark - UICollectionView delegate methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MemberListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MemberListCollectionViewCell" forIndexPath:indexPath];
[self configCell:cell cellForItemAtIndexPath:indexPath];
//[[cell contentView] setFrame:[cell bounds]];
//[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
return cell;
}
- (void) configCell:(MemberListCollectionViewCell *)cell cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
cell.backgroundColor = color;
}
Sorry for the wall of code but I wanted to provide you everything I also tried from
UICollectionView inside a UITableViewCell -- dynamic height?
UICollectionVIew inside a UITableViewCell how to get dynamic height with autolayout
Im also using a Layout for my CollectionView see https://github.com/fmitech/FMMosaicLayout maybe the layout applies after all the calculations and this causes my problem. Im running out of ideas may be somebody have one and can point me in the right direction.