Re: UITableView woes
Re: UITableView woes
- Subject: Re: UITableView woes
- From: BareFeetWare <email@hidden>
- Date: Tue, 03 Jul 2012 09:15:27 +1000
On 03/07/2012, at 6:49 AM, C.W. Betts <email@hidden> wrote:
> I'm trying to write an iOS app that displays sentences in a table, but UITableView cuts them off. Will I need to do a custom UITableViewCell or can I make UITableView behave? I want to either display them on two rows or have a scroll bar to see the rest of the sentence.
Putting a vertically scrollable object, such as a text view, into a table view is generally a bad idea and not recommended in the Apple documentation. The main problem is that a vertical scroll or swipe becomes ambiguous, ie should it scroll the table view or the text view?
It sounds like you need to cater for a variable number of lines in your text field. You can use a UILabel (change the "number of lines" property), UITextField or even a UITextView if you disable scrolling and ensure that the text object is high enough to fit all the text.
In a table view cell, you need to create the text field (programmatically or in a nib) which autoresizes when the table view cell content resizes. In your table view delegate, you need to adjust the size of the cell so that it fits the text it's about to display.
Something like this in your UITableViewDelegate (or UITableViewController):
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString* labelText = [self labelTextForIndexPath:indexPath];
CGSize textSize = [labelText sizeWithFont:self.textLabelFont constrainedToSize:CGSizeMake(textLabelWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
CGFloat maxTextHeight = textSize.height;
CGFloat rowHeight = maxTextHeight + self.verticalMargin;
if (rowHeight < self.tableView.rowHeight)
rowHeight = self.tableView.rowHeight;
return rowHeight;
}
To see an example of how this works:
1. Download and launch this app on an iPhone or iPod Touch:
http://itunes.apple.com/au/app/screen-nsw-film-location-scout/id516846577?mt=8
2. Tap "Metro Councils".
3. Tap "Ashfield..." or any other council.
4. Note how the different number of lines showing in each cell. ("Description" is deliberately truncated with an ellipsis in some.)
5. Rotate the iPhone to see the app recalculate the number of lines and adjust the cell heights accordingly.
Please reply to the list, not me directly.
Hope this helps,
Tom
Tom Brodhurst-Hill
BareFeetWare
--
iPhone/iPad/iPod and Mac software development, specialising in databases
email@hidden
--
Follow us on Twitter: http://twitter.com/barefeetware/
Like us on Facebook: http://www.facebook.com/BareFeetWare
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden