UITableView woes

  • 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.
  • If you want something potentially scrollable, you want a UITextField. And if you're using custom sub views then you should also be defining a custom UITableViewCell subclass. So yes, time to subclass and write custom stuff.

    Luke

    On Jul 2, 2012, at 1:49 PM, C.W. Betts 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.
  • Are you using the standard UITableViewCell or custom cells? I'm assuming they are cut off because the standard cells use a UILabel which displays only one line of text, shrinking it until it reaches the minimum size where, at that point, it just cuts off the rest of the sentence. You will need to use a UITextView most likely in your own subclass of UITableViewCell.

    -Laurent.
    --
    Laurent Daudelin
    AIM/iChat/Skype:LaurentDaudelin                 http://www.nemesys-soft.com/
    Logiciels Nemesys Software                    <laurent...>

    On Jul 2, 2012, at 13:49, C.W. Betts 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.
  • Or alternatively, setting the numberOfLines property on the text label to 0 and then calculating the height for each row for the string you'll be passing in.

    On Jul 2, 2012, at 3:22 PM, Laurent Daudelin <laurent...> wrote:

    > Are you using the standard UITableViewCell or custom cells? I'm assuming they are cut off because the standard cells use a UILabel which displays only one line of text, shrinking it until it reaches the minimum size where, at that point, it just cuts off the rest of the sentence. You will need to use a UITextView most likely in your own subclass of UITableViewCell.
    >
    > -Laurent.

    > On Jul 2, 2012, at 13:49, C.W. Betts 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.
  • On 03/07/2012, at 6:49 AM, C.W. Betts <computers57...> 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?m
    t=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
    <developer...>
    --
    Follow us on Twitter: http://twitter.com/barefeetware/
    Like us on Facebook: http://www.facebook.com/BareFeetWare
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today