简体   繁体   English

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

[英]Dynamic custom UITableViewCell's height based on label text length

I'm noob in Xcode and Objective-C and I need help to make a dynamic table view cell's height based oh how many character in the label. 我是Xcode和Objective-C中的菜鸟,我需要帮助才能根据标签中的字符数来制作动态表格视图单元格的高度。 So it will be like this: if textLabel char is more than 10 it will resize the listHeight to 50 else if textLabel char is more than 20 it will resize the listHeight to 70 and so on... 所以它会是这样的:如果textLabel char超过10,它会将listHeight的大小调整为50,否则如果textLabel char超过20,它会将listHeight的大小调整为70,依此类推......

This is how i code: 这是我编码的方式:

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

I'm trying this but not working: 我正在尝试这个但不工作:

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

Please help me, i'm dying to do this @_@ Thanks in advance 请帮助我,我很想这样做@ _ @提前感谢

Try this: 试试这个:

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

👍 Hope it works !!! 👍希望它有效!!!

An alternative to @Pratik's answer if you want to use dynamic text sizes would be something like a class method on the cell. 如果你想使用动态文本大小,@ Pratik的答案的替代方法就像是单元格上的类方法。

What we do at work is 我们在工作中做的是

+ (CGFloat)cellHeightForTitle:(NSString*)title inTableView:(UITableView*)tableView{
    UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    NSString *text = title ?: @"test"; // Just some text
    CGFloat hotizontalPadding = 0; // account for other content for calculating width of the cell
    CGFloat desiredWidth = tableView.bounds.size.width - hotizontalPadding;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];

    UILabel *label = [[UILabel alloc] init];

    label.attributedText = attributedText;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [label sizeThatFits:CGSizeMake(desiredWidth, CGFLOAT_MAX)];

    font = nil;
    attributedText = nil;

    return size.height;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态自定义UITableViewCell的高度基于Swift中的标签文本长度 - Dynamic custom UITableViewCell's height based on label text length in Swift 设置UITableViewCell高度和基于文本高度的单元格文本标签 - Setting UITableViewCell height and the cell's text label based on the text height 根据1个标签动态更改UITableViewCell的高度? - Change the height dynamically on UITableViewCell's based on 1 label? 在UITableViewCell中使用动态高度更改UILabel文本长度不能正确调整其大小 - Changing UILabel text length in UITableViewCell with dynamic height does not resize it correctly iOS:具有自定义uitableviewcell的动态高度 - IOS: dynamic height with a custom uitableviewcell 自定义 UITableViewCell 的动态高度问题 - Dynamic Height Issue for Custom UITableViewCell 动态uitableviewcell高度根据自定义单元格高度 - dynamic uitableviewcell height according to custom cell height 标签文本更改时,重新计算UITableViewCell的高度 - Recalculate the height of the UITableViewCell when the text of the label changes Xamarin Forms iOS自定义UITableViewCell动态高度 - Xamarin Forms iOS custom UITableViewCell dynamic height IB自定义UITableViewCell-单元格和detailText的动态高度 - IB Custom UITableViewCell - Dynamic Height for cell and detailText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM