Desmond
Desmond

Reputation: 5001

UITableViewCell does not wrap text

please look at my code, my cell label does not wrap text.

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


    UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyCellIdentifier] autorelease];


        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setClipsToBounds:NO];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        UILabel *label = [[UILabel alloc] init];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        [label setNumberOfLines:0];
        [label setTextColor:[UIColor darkGrayColor]];
        [label setShadowColor:[UIColor whiteColor]];
        [label setShadowOffset:CGSizeMake(0, 1)];
        [[cell contentView] addSubview:label];
        [label release];

    }
    UILabel *froglabel = (UILabel *)cell;  
    NSUInteger row = [indexPath row];
    CGSize textSize;
    CGSize labelSize = { 100, 20000 };
    [froglabel setText:genus];
    [froglabel setFont:detailFont];
    [froglabel setTextColor:[UIColor whiteColor]];
    textSize = [[froglabel text] sizeWithFont:[self detailFont] constrainedToSize:labelSize lineBreakMode:UILineBreakModeWordWrap];

Table view height for row at index

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

    NSUInteger row = [indexPath row];
    CGSize textSize;
    CGSize labelSize = { 300, 20000 };  // width and height of text area


    textSize = [[self genus] sizeWithFont:[self detailFont] constrainedToSize:labelSize lineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"%i = height %f", row, textSize.height);
    return textSize.height + 7;
    break;

enter image description here

Upvotes: 0

Views: 1509

Answers (2)

simongking
simongking

Reputation: 740

I think that this line is the culprit

UILabel *froglabel = (UILabel *)cell;  

You are basically casting the cell as the label which means you are assigning the text to the cells default text label using a deprecated method.You should consider the following:

UILabel *label;

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyCellIdentifier] autorelease];


    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setClipsToBounds:NO];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    label = [[[UILabel alloc] init] autorelease];

    [label setBackgroundColor:[UIColor clearColor]];
    [label setLineBreakMode:UILineBreakModeWordWrap];
    [label setNumberOfLines:0];
    [label setTextColor:[UIColor darkGrayColor]];
    [label setShadowColor:[UIColor whiteColor]];
    [label setShadowOffset:CGSizeMake(0, 1)];
    label.tag = 2;
    [[cell contentView] addSubview:label];
}
else
{
    label = (UILabel *)[cell.contentView viewWithTag:2];
}

NSUInteger row = [indexPath row];
CGSize textSize;
CGSize labelSize = { 100, 20000 };
[label setText:genus];
[label setFont:detailFont];
[label setTextColor:[UIColor whiteColor]];

Hopefully that helps.

Upvotes: 2

Or Ron
Or Ron

Reputation: 2343

you need to change this

[label setNumberOfLines:2];

It basically tells the label the maximum warping line

you also need to make sure that the frame height is big enough for 2 (or more) lines

Upvotes: 2

Related Questions