Uitableview 找不到我手机的高度

Uitableview 找不到我手机的高度,uitableview,ios7,uilabel,Uitableview,Ios7,Uilabel,我知道有很多链接可以获取单元格内标签的高度。我什么都试过了。。什么也没用 我的设想是: 我有一个单元格,在这个单元格中我有6个标签,这些标签中的文本是动态的,并且来自web服务 我不知道我做错了什么,我已经应用了所有方法来获得正确的动态高度: 我有两个问题: 1) 当tableview首次加载时,标签的高度不正确(根据nib)。 2) 当我重新加载表格视图时,它会给我动态高度,但它不正确 这是我的密码: 在控制器中,我每次计算单元高度时: -(float)saltDetailsCellHeigh

我知道有很多链接可以获取单元格内标签的高度。我什么都试过了。。什么也没用

我的设想是:

我有一个单元格,在这个单元格中我有6个标签,这些标签中的文本是动态的,并且来自web服务

我不知道我做错了什么,我已经应用了所有方法来获得正确的动态高度:

我有两个问题:

1) 当tableview首次加载时,标签的高度不正确(根据nib)。 2) 当我重新加载表格视图时,它会给我动态高度,但它不正确

这是我的密码:

在控制器中,我每次计算单元高度时:

-(float)saltDetailsCellHeight : (HKDrugSaltInfoModel *)saltInfoModel
{
    UIColor *_black=[UIColor blackColor];
    UIColor *_lightGray = [UIColor lightGrayColor];
    UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:13.0f];

    // salt Heading
    NSString * saltNameLabelString = [NSString stringWithFormat:@"%@ %@",saltInfoModel.saltName,saltInfoModel.strength];
    float hSaltNameLabelString =  [HKGlobal heightOfGivenText:saltNameLabelString ofWidth:300.0 andFont:font];

    // Salts string
    NSString * saltsString  = [NSString stringWithFormat:@"Pregnancy:%@ Lactation:%@ Lab:%@ Food:%@",saltInfoModel.ci_pg,
                               saltInfoModel.ci_lc,saltInfoModel.ci_lb,saltInfoModel.ci_fd];
    float hSaltsString =  [HKGlobal heightOfGivenText:saltsString ofWidth:300.0 andFont:font];

    // Usage Label
    NSString *usageString = [NSString stringWithFormat:@"Typical Usage: %@",saltInfoModel.lc];
    NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:usageString];
    NSInteger _stringLength=[usageString length];
    [attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(15, _stringLength-15)];
    float hUsageString =  [HKGlobal heightOfGivenText:[NSString stringWithFormat:@"%@", (NSString *)attrStr] ofWidth:300.0 andFont:font];


    // Side Effects label
    NSString *sideEffectsString = [NSString stringWithFormat:@"Side Effects: %@",saltInfoModel.se];
    NSMutableAttributedString* attrStrSide = [[NSMutableAttributedString alloc] initWithString:sideEffectsString];
    NSInteger _stringLengthSE = [sideEffectsString length];
    [attrStrSide addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLengthSE)];
    [attrStrSide addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
    [attrStrSide addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(15, _stringLengthSE-15)];
    float hSideEffectsString =  [HKGlobal heightOfGivenText:[NSString stringWithFormat:@"%@",(NSString *)attrStrSide] ofWidth:300.0 andFont:font];


    // Drug Interaction Label
    NSString *drugInteractionString = [NSString stringWithFormat:@"Drug Interaction: %@",saltInfoModel.di];
    NSMutableAttributedString* attrdrugInteractionStr = [[NSMutableAttributedString alloc] initWithString:drugInteractionString];
    NSInteger _strLenDrugInteraction = [drugInteractionString length];
    [attrdrugInteractionStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenDrugInteraction)];
    [attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 18)];
    [attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(18, _strLenDrugInteraction-18)];
    float hAttrdrugInteractionStr =  [HKGlobal heightOfGivenText:[NSString stringWithFormat:@"%@", (NSString*)attrdrugInteractionStr] ofWidth:300.0 andFont:font];

    // MechanismOfAction Label
    NSString *moaString = [NSString stringWithFormat:@"Mechanism Of Action: %@",saltInfoModel.moa];
    NSMutableAttributedString* attrMoaStr = [[NSMutableAttributedString alloc] initWithString:moaString];
    NSInteger _strLenMoa=[moaString length];
    [attrMoaStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenMoa)];
    [attrMoaStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 21)];
    [attrMoaStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(21, _strLenMoa-21)];
    float hAttrMoaStr =  [HKGlobal heightOfGivenText:[NSString stringWithFormat:@"%@", (NSString*)attrMoaStr] ofWidth:300.0 andFont:font];

    arrayOFLabelsHeights  = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithFloat:hSaltNameLabelString],
                             [NSNumber numberWithFloat:hSaltsString],
                             [NSNumber numberWithFloat:hUsageString],
                             [NSNumber numberWithFloat:hSideEffectsString],
                             [NSNumber numberWithFloat:hAttrdrugInteractionStr],
                             [NSNumber numberWithFloat:hAttrMoaStr],
                             nil];

    float cellHeight = hSaltNameLabelString+10+hSaltsString+10+hUsageString+10+hSideEffectsString+10+hAttrdrugInteractionStr+10+hAttrMoaStr;

    return cellHeight;

}
我在tableview中使用上述方法:heightForRowAtIndexpath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

            return [self saltDetailsCellHeight:[self.drugDetailModel.saltInfoArray objectAtIndex:indexPath.row-3]];

}
在索引路径行的单元格中,我传递arrayOFLabelsHeights数组,其中包含每个标签的高度:

 HKMedicineDetailInfoCell *medicineDetailInfoCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
            if(medicineDetailInfoCell == nil)
            {
                medicineDetailInfoCell = (HKMedicineDetailInfoCell *)[[[NSBundle mainBundle] loadNibNamed:@"MedicineDetailInfoCell" owner:self options:nil] objectAtIndex:0];
            }
            //Check whether array contains any object or not
            if([self.drugDetailModel.saltInfoArray count] > 0)
            {
                [medicineDetailInfoCell customiseLabels:[self.drugDetailModel.saltInfoArray objectAtIndex:indexPath.row-3] WithHeights:arrayOFLabelsHeights]; // since 3 rows are already there
            }
            return medicineDetailInfoCell;
方法customiseLabels:在我的自定义单元格类中声明如下(我认为可能存在代码重复,但我必须这样做,无法找到任何其他方法):

我仍然在标签之间留有很大的空间,下面是屏幕截图:


我什么都试过了。。在这个问题上苦苦挣扎了好几个小时(我认为这是一个小问题:()

我看不出您在哪里设置单元格的高度。您不应该在
heightforrowatinexpath
中调用一些模糊的函数,而应该返回单元格的高度

向数据源询问必要的字符串,然后用通常的方法计算高度,即使用
boundingRectWithSize:options:attributes:context:
。问题的根源是复杂的代码

-(void)customiseLabels:(HKDrugSaltInfoModel *)saltInfoModel WithHeights:(NSArray *)heightsArray
{

    UIColor *_black=[UIColor blackColor];
    UIColor *_lightGray = [UIColor lightGrayColor];
    //Helvetica Neue
    UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:13.0f];

    float h1 = [[heightsArray objectAtIndex:0] floatValue];

    frameTemp = self.saltNameLabel.frame;
    frameTemp.size.height = h1;
    self.saltNameLabel.frame = frameTemp;
    self.saltNameLabel.text = [NSString stringWithFormat:@"%@ %@",saltInfoModel.saltName,saltInfoModel.strength];


    frameTemp.origin.y += h1+10;

    float h2 = [[heightsArray objectAtIndex:1] floatValue];
    frameTemp.size.height = h2;
    self.secondDescriptiveLabel.frame = frameTemp;
    self.secondDescriptiveLabel.text = [NSString stringWithFormat:@"Pregnancy:%@ Lactation:%@ Lab:%@ Food:%@",saltInfoModel.ci_pg,saltInfoModel.ci_lc,saltInfoModel.ci_lb,saltInfoModel.ci_fd];

    frameTemp.origin.y += h2+10;

    float h3 = [[heightsArray objectAtIndex:2] floatValue];
    frameTemp.size.height = h3;
    self.usageLabel.frame = frameTemp;

    // Usage Label
    NSString *str = [NSString stringWithFormat:@"Typical Usage: %@",saltInfoModel.lc];
    NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    NSInteger _stringLength=[str length];
    [attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(15, _stringLength-15)];
    self.usageLabel.attributedText = attrStr;


    frameTemp.origin.y += h3+10;

    float h4 = [[heightsArray objectAtIndex:3] floatValue];
    frameTemp.size.height = h4;
    self.sideEffectsLabel.frame = frameTemp;

    // Side Effects label
    NSString *sideEffectsString = [NSString stringWithFormat:@"Side Effects: %@",saltInfoModel.se];
    NSMutableAttributedString* attrSideEffectsStr = [[NSMutableAttributedString alloc] initWithString:sideEffectsString];
    NSInteger _strLenSideEffects=[sideEffectsString length];
    [attrSideEffectsStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenSideEffects)];
    [attrSideEffectsStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 14)];
    [attrSideEffectsStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(14, _strLenSideEffects-14)];
    self.sideEffectsLabel.attributedText = attrSideEffectsStr;


    frameTemp.origin.y += h4+10;

    float h5 = [[heightsArray objectAtIndex:4] floatValue];
    frameTemp.size.height = h5;
    self.drugInteractionLabel.frame = frameTemp;

    // Drug Interaction Label
    NSString *drugInteractionString = [NSString stringWithFormat:@"Drug Interaction: %@",saltInfoModel.di];
    NSMutableAttributedString* attrdrugInteractionStr = [[NSMutableAttributedString alloc] initWithString:drugInteractionString];
    NSInteger _strLenDrugInteraction=[drugInteractionString length];
    [attrdrugInteractionStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenDrugInteraction)];
    [attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 18)];
    [attrdrugInteractionStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(18, _strLenDrugInteraction-18)];
    self.drugInteractionLabel.attributedText = attrdrugInteractionStr;

    frameTemp.origin.y += h5+10;

    float h6 = [[heightsArray objectAtIndex:5] floatValue];
    frameTemp.size.height = h6;
    self.moaLabel.frame = frameTemp;

    // MechanismOfAction Label
    NSString *moaString = [NSString stringWithFormat:@"Mechanism Of Action: %@",saltInfoModel.moa];
    NSMutableAttributedString* attrMoaStr = [[NSMutableAttributedString alloc] initWithString:moaString];
    NSInteger _strLenMoa=[moaString length];
    [attrMoaStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _strLenMoa)];
    [attrMoaStr addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, 21)];
    [attrMoaStr addAttribute:NSForegroundColorAttributeName value:_lightGray range:NSMakeRange(21, _strLenMoa-21)];
    self.moaLabel.attributedText = attrMoaStr;

    NSLog(@"height of cell : %f", frameTemp.origin.y + self.moaLabel.frame.size.height);
}