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.
-
Add a UILabel as a subview to my custom UITableCell subclass, and set the
numberOfLinesproperty to either a calculated number based on the height above (say, 6), or to 0 (theoretically unlimited). ThenumberOfLinesis ignored; I see either 1 or 2 lines, and no more. -
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?
You might want to look at the
userInteractionEnabledproperty of theUITextView. That should allow input to be passed through to theUITableViewso you get scrolling.