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 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.
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 userInteractionEnabled
property of the UITextView
. That should allow input to be passed through to the UITableView
so you get scrolling.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With