【问题标题】:Autoresize cell according to UILabel text and image like Chatting根据 UILabel 文本和图像自动调整单元格大小,例如聊天
【发布时间】:2016-08-31 07:09:01
【问题描述】:

我的问题是如何调整UITableViewCell 中的大文本和图像的固定大小。
根据 WhatsApp 等数据,我需要将单元格设为 autoresize。我该怎么做。 当我使用UILabel 时,我的单元格正在调整大小,但如果是图像,它会搞砸一切。

请给我建议。提前致谢。

@ddb 这是我正在使用的源代码。

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

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [adminDataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"admincell";
    adminCell =[tableView dequeueReusableCellWithIdentifier:identifier];
    if (adminCell==nil) {
        adminCell=[[AdminPostTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    adminCell.selectionStyle=UITableViewCellSelectionStyleNone;
    [adminCell setBackgroundColor:[UIColor clearColor]];
    NSMutableDictionary *getAdminDataDictionary=[adminDataArray objectAtIndex:indexPath.row];
    if ([getAdminDataDictionary objectForKey:@"TEXT"])
    {
        adminCell.lblSenderAdminPage.text=[getAdminDataDictionary objectForKey:@"TEXT"];
        adminCell.lblSenderAdminPage.textColor=[UIColor blackColor];
        adminCell.lblSenderAdminPage.backgroundColor=[UIColor whiteColor];
        adminCell.lblSenderAdminPage.preferredMaxLayoutWidth=305;
        adminCell.lblSenderAdminPage.layer.masksToBounds=YES;
        adminCell.lblSenderAdminPage.layer.cornerRadius=5;
        adminCell.lblSenderAdminPage.layer.borderWidth=1.0f;
        adminCell.lblSenderAdminPage.layer.borderColor=[UIColor blackColor].CGColor;
    }
    else if ([getAdminDataDictionary objectForKey:@"IMAGE"]){        
        adminCell.imgSenderAdminPage.image=[getAdminDataDictionary objectForKey:@"IMAGE"];
    }
    return adminCell;
}

【问题讨论】:

标签: ios autoresize


【解决方案1】:

你可以试试

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

    CGSize messageSize  // Your message size
    if (img) {
        CGFloat imgHeight = img.size.height;  // Image size
        return messageSize.height + imgHeight + 2* textMarginVertical;
    } else {
        return messageSize.height + 2* textMarginVertical;
    }
}

在类的顶部声明

static CGFloat textMarginVertical = 7f;

编码愉快..

【讨论】:

  • 这里的“img”是什么?
  • 但是当我将大小设置为:-
  • CGFloat imgHeight =admincell.imgSenderAdminPage.size.height;
  • @Abhishek Sharma 不是 UIImageView。它是 UIImage。这是 CGFloat imgHeight =admincell.imgSenderAdminPage.size.height 工作???
  • 不,先生。请给我一些建议?
猜你喜欢
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 2021-09-28
  • 1970-01-01
  • 2013-11-25
  • 2013-01-02
  • 2017-02-28
相关资源
最近更新 更多