How can i resize UITableViewCell for how much text

2020-06-28 09:07发布

I get some text from the server and i put it to the UILabel that it will be add to the UITableViewCell and this text change every time it can be tiny or large or with multiple line. my question is how can i make this UILabel to fit the text automatically (multiple line) and resize the UITableViewCell to fit the UILabel?

2条回答
SAY GOODBYE
2楼-- · 2020-06-28 09:42

You can do the following in your tableView's datasource:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  // for the sake of illustration, say your model for this table is just an array of strings you wish to present.
  // it's probably more complex, but the idea is to get the string you want to present for the
  // cell at indexPath
  NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row];

  // you can get fancier here, and dynamically get the font from the UITextView prototype
  // but for simplicity, just copy the font size you configured the text view with in your nib
  CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]];

  // this is a little funky, because for it to be just right, you should format your prototype
  // cell height to be a good-looking height when your text view has a zero height.
  // the basic idea here is that the cell will get taller as the text does.
  return tableView.rowHeight + size.height;
}

Then, when you configure the cell, get the string and the size the same way, and set the UITextView frame to match the size

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


      NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row];
      CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]];
      UITextView *myTextView = (UITextView *)[cell viewWithTag:kMY_TEXT_VIEW_TAG];  // make this tag match a tag you give the text view in the prototype cell

      myTextView.frame = CGRectMake(myTextView.frame.origin.x, myTextView.frame.origin.y, size.width, size.height);
      // the rest of your configure cell
}
查看更多
做个烂人
3楼-- · 2020-06-28 09:45

You need to implement this:-

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

{
    CGSize labelsize;
    UILabel * textDesc1 = [[UILabel alloc] init];
    [textDesc1 setNumberOfLines:0];
    textDesc1.text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text
    [textDesc1 setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
    labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    [textDesc1 release];
    return (CGFloat)labelsize.height; 


}
- (UITableViewCell *)tableView:(UITableView *)tbleView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize labelsize;
    UILabel *commentsTextLabel = [[UILabel alloc] init];;
    [commentsTextLabel setNumberOfLines:0];
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    NSString *text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text
commentsTextLabel.text=text;
    [commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    commentsTextLabel.frame=CGRectMake(0, 0, 320, labelsize.height);
    [cell.contentView addSubview:commentsTextLabel];
    [commentsTextLabel release];
}
查看更多
登录 后发表回答