tags:

views:

3136

answers:

3

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?

+3  A: 

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.

Stephen Darlington
That seems to work, with scrolling disabled. I think somewhere my UITextView is not getting appropriately sized though.
Richard Campbell
Yeah, eventually was able to get the right autosizing set of parameters to make that work.
Richard Campbell
A: 

While playing with userInteractionEnabled=NO, scrollEnabled=NO, and getting the right autosizing parameters set in IB worked, I think that going with a UILabel with numberOfLines=0 and the same autosizing parameters is ultimately a better idea, for the next person.

Richard Campbell
+1  A: 

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

Tim Stephenson