我的每个单元格都有一个UITableView
UICollectionView
。我的问题是UITableView
的高度从未采用UICollectionView
内容的高度。当我将UICollectionView
更改为标签时,UITabelViewCell
根据标签文字的高度正确采用了高度。我尝试了很多不同的东西,但没有任何效果,我也不想计算heightForRowAtIndexPath
中每个单元格的高度,因为我不知道那个时刻的内容,因为我需要它真正的泛型因为一系列不同的细胞。我现在拥有的是:
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;
}
很抱歉代码墙,但我想为您提供我尝试过的所有内容
UICollectionView inside a UITableViewCell -- dynamic height?
UICollectionVIew inside a UITableViewCell how to get dynamic height with autolayout
我也在使用我的CollectionView的布局,请参阅https://github.com/fmitech/FMMosaicLayout也许布局适用于所有计算后,这会导致我的问题。我没有想法可能有人有一个,可以指出我正确的方向。