无法找到我的牢房的高度

时间:2013-11-21 14:44:08

标签: uitableview ios7 uilabel

我知道有很多链接可以获得单元格内标签的高度。我已经尝试了所有的事情......没有任何工作......

我的情景是:

我有一个单元格,在这个单元格中我有6个标签,这些标签中的文字是动态的,来自网络服务。

我不知道我做错了什么,我已经应用了所有东西来获得动态高度:

我有两个问题:

1)当tableview加载第一次标签的高度不合适时(根据笔尖)。 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:在我的自定义单元格类中声明(我认为会有代码重复,但我必须这样做,不能找到任何其他方式):

-(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);
}

我仍然在标签之间有大空格,这里是截图:

enter image description here enter image description here

我已经尝试了所有的东西..并且在这个问题上苦苦挣扎了好几个小时(我认为是一个小问题:()

1 个答案:

答案 0 :(得分:0)

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

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