2011-08-22 74 views
0

我目前正在试图让一个UITableView使用基于数组中的文本的自定义单元格高度。我看到的问题是文本似乎在点上挤压而不是填充自定义单元格的整个长度。这导致文本在单元格上重叠,有时在它们下面消失。与UILabel和随机文本长度的UITableViewCell高度的问题

下面是我用于确定单元格高度的代码,我不确定标准UILabel文本高度,但是这似乎在字体的高度I上运行良好。

if (indexPath.section == 0) { 

    NSString *text = [[self.recipeDict objectForKey:@"Ingredients"] objectAtIndex:[indexPath row]]; 

    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = size.height; 

    return height + 20; 
} 

同样以防万一细胞的产生或者是阻我的问题或只是帮助你知道发生了什么事情,这里有太多。

UITableViewCell *cell; 
UITableViewCell *ingredientCell; 
UITableViewCell *methodCell; 

if (indexPath.section == 0) { 
    UILabel *ingredientText; 

    static NSString *ingredientCellIdentifier = @"Ingredient Cell"; 

    ingredientCell = [tableView dequeueReusableCellWithIdentifier: ingredientCellIdentifier]; 

    if (ingredientCell == nil) 
    { 
     ingredientCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: ingredientCellIdentifier] autorelease]; 

     ingredientText = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 290, 44)]; 
     [ingredientText setLineBreakMode:UILineBreakModeWordWrap]; 
     [ingredientText setNumberOfLines:0]; 
     [ingredientCell.contentView addSubview:ingredientText]; 

     ingredientText.tag = 1; 

     [ingredientText release]; 

     ingredientCell.contentMode = UIViewContentModeRedraw; 
    } 

    ingredientText = (UILabel*)[ingredientCell viewWithTag:1]; 
    ingredientText.text = [[self.recipeDict objectForKey:@"Ingredients"] objectAtIndex: indexPath.row]; 
    [ingredientText sizeToFit]; 

    return ingredientCell; 
} 

这是我在解决这一问题的第二次尝试,但它似乎是超出了我的能力,电流,所以我欢迎智慧通过经验获得。

更新 -

经过进一步调查似乎的UILabel ingredientText被调整导致了问题。

它的起始高度与所需显示的文字一样高,但是当该标签重新绘制时,对于需要更多线条的另一段文本而言,该标签不会再缩小。似乎sizeToFit方法使用可用高度优先化,而不是占用宽度和缩小高度。

现在我已经在sizeToFit之后再次设置了宽度,该宽度解决了问题,但它会导致奇数间距,具体取决于标签的高度。

回答

0

解决这个问题是在单元高度的方法在不断的指定宽度没有匹配稍后在cellForRowAtIndexPath方法中指定的单元格文本的框架。

一旦这些都是相同的问题自行解决。

不同的常量大小导致报告的UILabel高度不一致和不准确。这导致设置不正确的表行高度。

0

几件事情:

  • 你还没有为的UILabel的字体,这意味着你上浆一个未知的高度
  • 你需要设置你的UILabel自动调整面罩,使之当单元格的高度改变

这里是工作码量(测试):

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 

     NSString *text = [_items objectAtIndex:[indexPath row]]; 

     CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

     CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

     CGFloat height = size.height; 

     return height + 20; 
    } 

    return tableView.rowHeight; 
} 

#pragma mark - UITableViewDatasource 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell; 
    UITableViewCell *ingredientCell = nil; 
    UITableViewCell *methodCell; 

    if (indexPath.section == 0) { 
     UILabel *ingredientText; 

     static NSString *ingredientCellIdentifier = @"Ingredient Cell"; 

     ingredientCell = [tableView dequeueReusableCellWithIdentifier: ingredientCellIdentifier]; 

     if (ingredientCell == nil) 
     { 
      ingredientCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: ingredientCellIdentifier] autorelease]; 

      ingredientText = [[UILabel alloc] initWithFrame:ingredientCell.bounds]; 
      ingredientText.font = [UIFont systemFontOfSize:12.0f]; 
      ingredientText.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
      [ingredientText setLineBreakMode:UILineBreakModeWordWrap]; 
      [ingredientText setNumberOfLines:0]; 
      [ingredientCell.contentView addSubview:ingredientText]; 

      ingredientText.tag = 1; 

      [ingredientText release]; 

      ingredientCell.contentMode = UIViewContentModeRedraw; 
     } 

     ingredientText = (UILabel*)[ingredientCell viewWithTag:1]; 
     ingredientText.text = [_items objectAtIndex: indexPath.row]; 

     return ingredientCell; 
    } 

} 
  • 确保你总是可以针对的tableView返回值:的cellForRowAtIndexPath:和的tableView:heightForRowAtIndexPath:方法
+0

我注意到你设置ingredientCell无权在cellForRowAtIndexPath:方法的开始。看起来会强制整个单元在每次使用时被重新加载。这是做这件事的最好方式,而不是调整现有单元的大小? –

+0

我试过这段代码,当它们被快速重绘时它仍然没有正确绘制单元格。当我快速地按下我的UITableView时,我会看到这个问题。当它们重新绘制时,它们看起来比它们应该更窄,约为宽度的一半。 –

+0

我确实将“返回成分细胞”移动到“if”块之外;所以它应该总是被初始化为'nil'来避免垃圾被返回。我将它移回原来的代码。 –