我尝试使用Autolayout构建不同的自定义单元格,但我总是得到默认高度44.为了更好地理解:
GTBlockView是我所有自定义单元的超类,它是一个UITableViewCell! 这适用于iOS 7和iOS 8,因此我无法使用新的自动功能... 我永远不知道单元格应该是多少,因为我从服务器获取数据!所以重要的是,一切都是动态的!
- (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;
}
以下是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;
}
在这里,我打电话给我的一个带有约束的自定义单元格:
- (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];
}
但就像我上面所说,我总是得到默认高度,但在这种情况下应该是100.0f ......
答案 0 :(得分:0)
在viewDidLoad中添加此内容
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 101