如何在 UITableViewCell 中设置动态标签大小

How to set dynamic label size in UITableViewCell

我有 TableViewCell 并通过 Web 服务加载数据 (JSON),数据存储在不同的数组中,数据加载到表视图中。我想数组数据大小有时大有时小。那么如何管理标签大小。我试过很多例子,但没有制作动态标签。请帮忙,谢谢。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"tableview cell");
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"event"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [img objectAtIndex:indexPath.row]]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    cell.img.image =image;

    cell.lbl_1.text=[NSString stringWithFormat:@"%@",[title objectAtIndex:indexPath.row]];

    cell.lbl_2.text=[NSString stringWithFormat:@"%@",[des objectAtIndex:indexPath.row]];
    cell.lbl_3.text=[NSString stringWithFormat:@"%@",[evnt_ary objectAtIndex:indexPath.row]];

    return  cell;
}

请检查UITableViewAutomaticDimension并像这样使用它:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension

确保你的约束是正确的。

首先,为您的 tableView 使用自定义单元格。如果直接在 tableView 上使用 label,动态改变它的大小时会导致加载问题。

然后在"cellForRowAtIndexPath"中,您可以更改它的属性。例如,如果 "TableCell" 是您的自定义单元格,则

 TableCell *cell = (NBEServicesCell*)[tableView dequeueReusableCellWithIdentifier:"@yourIdentifier" forIndexPath:indexPath];

    //Assigning each row a number.
[cell.yourLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:14.0]];
cell.yourLabel.numberOfLines = 0;
cell.yourLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.yourLabel sizeToFit];
cell.yourlabel.frame.size.width = newWidth;
cell.yourlabel.frame.size.height = newHeight;
CGSize maximumSize = CGSizeMake(width_specified, CGFLOAT_MAX);


CGSize myStringSize = [myString sizeWithFont:myFont 
                           constrainedToSize:maximumSize 
                               lineBreakMode:self.myLabel.lineBreakMode];

您可以计算字符串所需的高度以适合给定宽度的标签或文本视图。

您还需要指定字符串的字体。 IE。字符串容器中使用的字体。

在为该标签分配文本后,您只需编写 [yourCell.yourlabel sizeToFit]

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCell.yourlabel.text = @"text whatever you want to set";
    [yourCell.yourlabel sizeToFit];
}

同时将文本分配给 yourCell.yourlabel 并在 heightForRowAtIndexPath 中写入 [yourCell.yourlabel sizeToFit]。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCustomCell *yourCellObj = (yourCustomCell *)[self tableView:self.tableViewObj cellForRowAtIndexPath:indexPath];

    yourCellObj.yourlabel.text = @"text whatever you want to set";
    [yourCellObj.yourlabel sizeToFit];

    return (yourCellObj.yourlabel.frame.origin.y + yourCellObj.yourlabel.frame.size.height);
}

您必须根据标签的内容(文本)获得动态宽度和高度,这样您就可以使用以下函数 return CGSize,您只需传递您的标签参数。

- (CGSize ) messageSizeExact:(UILabel *) msgLabel
{

    CGRect textRect = [msgLabel.text boundingRectWithSize:(CGSize){280.0, CGFLOAT_MAX}
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName:msgLabel.font}
                                            context:nil];

    return textRect.size;
}

请走table

的出口
@property (strong, nonatomic) IBOutlet UITableView *view_tableView;


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view_tableView.estimatedRowHeight = 50;
    self.view_tableView.rowHeight = UITableViewAutomaticDimension;
    // Do any additional setup after loading the view, typically from a nib.
}


#pragma mark - TableView Delegate And DataSource



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    UILabel *label =(UILabel *)[cell viewWithTag:5000];

    label.text=@"Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.2016-07-15 15:22:28.819 Time'nTask[257:14278] Notification settings registered2016-07-15 15:22:28.820 Time'nTask[257:14278] Successfully registered for remote notifications With device Token 68048cc293d695fd3220cf63da3baa685675126ecd72af0547b760ee31571b62";

    return cell;
}

记住设置标签行数 = 0