【问题标题】:how can I adjust label height according to the text content in objective c如何根据目标c中的文本内容调整标签高度
【发布时间】:2018-12-01 18:32:42
【问题描述】:

下面是不工作的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    MessageObject *currentdatas = [self.objectHolderArray objectAtIndex:indexPath.row];

    cell.Name_Label.text = currentdatas.Name;
    //Here I wanted to adjust my label height according to my message received

    cell.Message_Label.text = currentdatas.MSG;
    [cell.Message_Label sizeToFit];

    NSTimeInterval timeInterval = currentdatas.time/1000;
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-mm-yyyy"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    NSLog(@"My date %@",dateString);
    cell.Time_Label.text = dateString;

    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:currentdatas.Profile]]];
    // Image Rectangle to circular
    cell.Image_View.image = image;
    cell.Image_View.layer.cornerRadius = cell.Image_View.frame.size.width/2;
    cell.Image_View.clipsToBounds = YES;

    return cell;
}

【问题讨论】:

  • 正确的代码格式有多难?

标签: ios objective-c uitableview uilabel


【解决方案1】:

添加此方法。设置标签行数为0。

在tableview中添加这段代码

self.tableView.estimatedRowHeight = 80;
self.tableView.rowHeight = UITableViewAutomaticDimension;

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

   return UITableViewAutomaticDimension; 
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

【讨论】:

  • @IOS 嘿需要帮助
  • @RKios 告诉我有什么问题
  • @IOS 如何在标签扩展时动态更改我的 collectionView 单元格
  • @IOS 我已经在聊天中发送了链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多