Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to implement a variable-sized multi-line UITableCell?

Tags:

cocoa-touch

I'm trying to display a table full of twitter statuses (yes, this is the Stanford Presence 2 assignment), which are variably sized. I can relatively easily determine the appropriate height for my rows with code that approximates (from accompanying lecture 9):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath
{
    NSString *text = ...;
    UIFont *font = [UIFont systemFontOfSize:...];
    CGSize withinSize = CGSizeMake(tableView.width, 1000];
    CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];

    return size.height + somePadding;
}

I have tried two approaches (and some tweaks to both) to get a multi-line word-wrapping field of text into my table row.

  1. Add a UILabel as a subview to my custom UITableCell subclass, and set the numberOfLines property to either a calculated number based on the height above (say, 6), or to 0 (theoretically unlimited). The numberOfLines is ignored; I see either 1 or 2 lines, and no more.

  2. Add a read-only UITextView as a subview. This has the problem that the UITextView eats my scrolling; I end up scrolling inside a UITextView row instead of moving smoothly from row to row. If I disable scrolling on the UITextView, I end up being unable to scroll at all.

This is a pretty common thing to do; what's the best way to accomplish it?

like image 537
Richard Campbell Avatar asked Jan 12 '09 14:01

Richard Campbell


2 Answers

You might want to look at the userInteractionEnabled property of the UITextView. That should allow input to be passed through to the UITableView so you get scrolling.

like image 89
Stephen Darlington Avatar answered Dec 31 '22 19:12

Stephen Darlington


Here's a link to a blog I posted on this subject. I used a UILabel with numberOfLInes = 0. I hope this will be of some help.

Sample Project with Variable Sized UITableViewCell

like image 37
Tim Stephenson Avatar answered Dec 31 '22 18:12

Tim Stephenson