agentfll agentfll - 1 month ago 7
iOS Question

Make UITableViewCell expand when text exceeds space

I have a

UITableViewCell
and it is
UITableViewCellStyleDefault
. When I try to put text longer than the
UITableViewCell
, it truncates it. How do I make the cell expand to accommodate this text?

Answer

You can not expand cell's width more than iphone's screen... What you can do is 1> make font smaller 2> make your text multiple lines

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    CGRect contentRect = CGRectMake(80.0, 0.0, 240, 40);
    UILabel *textView = [[UILabel alloc] initWithFrame:contentRect];

    textView.text = mytext;
    textView.numberOfLines = 2;
    textView.textColor = [UIColor grayColor];
    textView.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:textView];
    [textView release];
Comments