简体   繁体   中英

UILabel height inside UITableViewCell

I'm trying to create a tableview where the height of the cells are dynamic.

So far I manage to set the height of the cells depending on the custom UILabel I've added inside.

With the regular cell.textLabel it works fine, but when I use my own label something goes wrong. I only see half the label, but when I scroll up and down, sometimes the label extends and shows all the text... You can see where the label should end in the image.

Image

This is the text inside the cellForRowAtIndexPath :

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell.
Car *carForCell = [cars objectAtIndex:indexPath.row];

UILabel *nameLabel = [[UILabel alloc] init];
nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.numberOfLines = 0;
nameLabel.text = carForCell.directions;
[nameLabel sizeToFit];

[nameLabel setBackgroundColor:[UIColor greenColor]];


return cell;

Unless you have typos in the code you posted, you don't seem to be adding the label to the cell at all. You also seem to be creating a new label every time, and then replacing the contents of your nameLabel pointer with the cell's view (which will always be nil).

Try doing something like this first and then see how it looks:

static NSString *CellIdentifier = @"Cell";

UILabel *nameLabel;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    nameLabel = [[UILabel alloc] init];
    nameLabel.tag = 100;

    nameLabel.numberOfLines = 0;
    [nameLabel setBackgroundColor:[UIColor greenColor]];

    [cell.contentView addSubview:nameLabel];
}
else {
     nameLabel = (UILabel *)[cell viewWithTag:100];
}

// Configure the cell.
Car *carForCell = [cars objectAtIndex:indexPath.row];

nameLabel.text = carForCell.directions;
[nameLabel sizeToFit];

return cell;

You will also need to tell the tableView what size each cell needs to be using the tableView:heightForRowAtIndexPath: delegate method. That will mean getting the relevant Car object again and calculating the height using sizeWithFont:sizeWithFont:forWidth:lineBreakMode:

How are you setting the height of the cell? It should be done in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

You should calculate and return the height of the UITableViewCell in the following method:

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

Here you should do your initial calculation of how high your cell should be.

For example:

CGSize textSize = [myString sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(320, 9999)];
return textSize.height;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM