找不到我的手机的高度

Posted

技术标签:

【中文标题】找不到我的手机的高度【英文标题】:cant find height for my cell 【发布时间】:2013-11-21 14:44:08 【问题描述】:

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

我的情况是:

我有一个单元格,在这个单元格中我有 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);

标签之间的空格还是很大,截图如下:

我已经尝试了每一件事.. 并且在这个问题上挣扎了好几个小时(我认为这是一个小问题 :()

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于找不到我的手机的高度的主要内容,如果未能解决你的问题,请参考以下文章

我的手机找不到黏贴板。

安卓13系统升级后找不到插件

在我的手机上找不到 apk 使用的本机库

为啥我的项目在手机中将其作为渐进式 Web 应用程序下载时找不到一些图像?

手机里的文件管理器找不到文件

为啥我的手机QQ显示的文字是口口口,而且存储卡里找不到resource文件夹?