动态自定义UITableViewCell的高度基于标签文本长度

Edw*_*ard 3 height objective-c uitableview ios

我是Xcode和Objective-C中的菜鸟,我需要帮助才能根据标签中的字符数来制作动态表格视图单元格的高度.所以它将是这样的:如果textLabel char超过10,它将把listHeight的大小调整为50,否则如果textLabel char超过20,它会将listHeight的大小调整为70,依此类推......

这是我编码的方式:

NSLong *text;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellID = @"ChatTableViewCell";
   ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];

   if (cell == nil){
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatTableViewCell" owner:self options:nil];
      cell = [nib objectAtIndex:0];
   }

   Chat * chatObject;
   chatObject = [chatArray objectAtIndex:indexPath.row];
   cell.nameChat.text = chatObject.name;
   cell.messageChat.text = chatObject.message;
   cell.messageChat.lineBreakMode = UILineBreakModeTailTruncation;
   cell.messageChat.numberOfLines = 0;
   cell.dateChat.text = chatObject.time_entry;

   //attached foto
   NSString *setPhoto=chatObject.photo;
   [cell.imageChat setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/summit/www/media/user/photo/%@",setPhoto]]]]];
   cell.imageChat.layer.cornerRadius=cell.imageChat.frame.size.height /2;;
   cell.imageChat.layer.masksToBounds = YES;
   cell.imageChat.layer.borderWidth = 0;
   if (cell.imageChat.image==nil) {
      cell.imageChat.image=[UIImage imageNamed:@"profile.png"];
   }

   text=[cell.messageChat.text length];
   return cell;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试这个但不工作:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CGFloat height;
   if(text>=10){
      height=100;
   }
   else
      height=70;
   return height;
}
Run Code Online (Sandbox Code Playgroud)

请帮助我,我很想这样做@ _ @提前感谢

NSP*_*tik 6

试试这个:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Fetch yourText for this row from your data source..
    NSString *yourText = [yourArray objectAtIndex:indexPath.row];

    CGSize labelWidth = CGSizeMake(300, CGFLOAT_MAX); // 300 is fixed width of label. You can change this value
    CGRect textRect = [visitorsPerRegion boundingRectWithSize:labelWidth options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"CenturyGothic" size:16.0]} context:nil];

    /* Here, you will have to use this requiredSize 
       and based on that, adjust height of your cell.
       I have added 10 on total required height of
       label and it will have 5 pixels of padding 
       on top and bottom. You can change this too. */

    int calculatedHeight = textRect.size.height+10;
    return (float)calculatedHeight;
}
Run Code Online (Sandbox Code Playgroud)

希望它有效!!!

  • sizeWithFont:constrainedToSize:lineBreakMode:在iOS 7中折旧.您应该使用boundingRectWithSize:options:attributes:context:而不是. (2认同)
  • 现在这种方法已被弃用,请在上面的地方使用"约束" (2认同)