[英]Dynamic Height for Custom Cell with Autolayout
I try to build different Custom Cells with Autolayout but i always get the default height 44. For better understanding: 我尝试使用“自动布局”构建不同的“自定义单元格”,但我始终使用默认高度44。为了更好地理解:
GTBlockView is my Superclass for all the Custom Cells and its a UITableViewCell! GTBlockView是我的所有自定义单元格的超类,它是一个UITableViewCell! This is for iOS 7 and iOS 8, so i cant use the new Automatic Functions... I never know how heigh the cell should be, because i get the Data from my Server!
这适用于iOS 7和iOS 8,因此我无法使用新的自动功能...我永远不知道单元格的高度,因为我是从服务器获取数据的! So its important, that everything is full dynamically!
因此,重要的是,一切都要动态地充满!
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self heightForBasicCellAtIndexPath:indexPath];
}
- (CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath {
static GTBlockView *sizingCell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
});
sizingCell = [self configureBasicCell:sizingCell atIndexPath:indexPath];
return [self calculateHeightForConfiguredSizingCell:sizingCell];
}
- (CGFloat)calculateHeightForConfiguredSizingCell:(GTBlockView *)sizingCell
{
sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), 0.0f);
[sizingCell.contentView setNeedsLayout];
[sizingCell.contentView layoutIfNeeded];
CGSize size = [sizingCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
NSLog(@"HÖHE: %f",size.height);
return size.height;
}
- (GTBlockView *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self basicCellAtIndexPath:indexPath];
}
- (GTBlockView *)basicCellAtIndexPath:(NSIndexPath *)indexPath {
GTBlockView *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[GTBlockView alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[self configureBasicCell:cell atIndexPath:indexPath];
return cell;
}
- (GTBlockView*)configureBasicCell:(GTBlockView *)cellBlock atIndexPath:(NSIndexPath *)indexPath {
GFBlock *block = [self.node.blocks objectAtIndex:indexPath.row];
cellBlock = [[GTAppController sharedInstance] createInterfaceForBlock:block];
return cellBlock;
}
Here is the important Method for the Cell: 这是该单元格的重要方法:
- (GTBlockView *)createInterfaceForBlock:(GFBlock *)block {
NSString* className = [self.interfacesDict objectForKey:NSStringFromBlockType(block.blockTypeId)];
Class c = nil;
if (className == nil)
{
c = [GTGenericBlockView class];
} else {
//Generate the specific Custom Cell Class
c = NSClassFromString(className);
}
GTBlockView* instance = [(GTBlockView *)[c alloc] initWithBlock:block];
return instance;
}
And here i call one of my Custom Cells with the Constraints: 在这里,我用约束称呼我的自定义单元格之一:
- (id)initWithBlock:(GFBlock *)block {
self = [super initWithBlock:block];
if (self) {
self.backgroundColor = GTDefaultBackgroundColor;
self.button = [[UIButton alloc]init];
self.button.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:self.button];
}
return self;
}
- (void)updateConstraints
{
if (!self.didSetupConstraints)
{
// 1. Create a dictionary of views
NSDictionary *viewsDictionary = @{@"buttonView":self.button};
NSDictionary *metrics = @{@"vSpacing":@0, @"hSpacing":@0};
// 3. Define the buttonView Position
NSArray *constraint_POS_V_SV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[buttonView]"
options:0
metrics:metrics
views:viewsDictionary];
NSArray *constraint_POS_H_SV = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[buttonView]-10-|"
options:0
metrics:metrics
views:viewsDictionary];
[self.contentView addConstraints:constraint_POS_V_SV];
[self.contentView addConstraints:constraint_POS_H_SV];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[buttonView(==100)]"
options:0
metrics:nil
views:viewsDictionary];
[self.button addConstraints:constraint_V];
self.didSetupConstraints = YES;
}
[super updateConstraints];
}
But like i said above, i always get the Default Height, but it should be 100.0f in this Case.... 但是就像我上面说的,我总是得到默认高度,但是在这种情况下应该是100.0f。
Add this in viewDidLoad 在viewDidLoad中添加
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 101
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.