Custom UITableView cell label with ARC and storyboards

  • I've put together a TVC with a custom TVC cell class consisting mainly of UILabels, but have the problem where the labels will have varying amounts of lines of text.

    The main issue is that in iOS 5.x, UILabels still can't be vertically aligned to the top.

    There is a fair amount of source that explains how to add a label as a subView, but it is all non ARC code so, as cells get reused, the UILabel will get recreated whenever cellForRowAtIndexPath is called.

    Does anyone have ideas on when to remove the last subview of the content cell or how to check for presence of the view already so that It doesn't get to allocated again every time the cell displays in the cellForRowAtIndexPath code?

    Thanks,
    - Alex Zavatone
  • On Jul 29, 2012, at 7:02 PM, Alex Zavatone <zav...> wrote:

    > I've put together a TVC with a custom TVC cell class consisting mainly of UILabels, but have the problem where the labels will have varying amounts of lines of text.
    >
    > The main issue is that in iOS 5.x, UILabels still can't be vertically aligned to the top.
    >
    > There is a fair amount of source that explains how to add a label as a subView, but it is all non ARC code so, as cells get reused, the UILabel will get recreated whenever cellForRowAtIndexPath is called.
    >
    > Does anyone have ideas on when to remove the last subview of the content cell or how to check for presence of the view already so that It doesn't get to allocated again every time the cell displays in the cellForRowAtIndexPath code?

    ARC really doesn't have anything to do with this. But my question would be why are you adding new labels in cellForRowAtIndexPath when you've already created a subclass of UITableViewCell? Have your subclass create (and maintain a reference) to these labels and never recreate them again.
    --
    David Duncan
  • On Jul 29, 2012, at 10:30 PM, David Duncan wrote:

    > On Jul 29, 2012, at 7:02 PM, Alex Zavatone <zav...> wrote:
    >
    >> I've put together a TVC with a custom TVC cell class consisting mainly of UILabels, but have the problem where the labels will have varying amounts of lines of text.
    >>
    >> The main issue is that in iOS 5.x, UILabels still can't be vertically aligned to the top.
    >>
    >> There is a fair amount of source that explains how to add a label as a subView, but it is all non ARC code so, as cells get reused, the UILabel will get recreated whenever cellForRowAtIndexPath is called.
    >>
    >> Does anyone have ideas on when to remove the last subview of the content cell or how to check for presence of the view already so that It doesn't get to allocated again every time the cell displays in the cellForRowAtIndexPath code?
    >
    >
    > ARC really doesn't have anything to do with this. But my question would be why are you adding new labels in cellForRowAtIndexPath when you've already created a subclass of UITableViewCell? Have your subclass create (and maintain a reference) to these labels and never recreate them again.

    Well, because I could not get setting the frame size of a storyboarded UILabel to do anything at all in the
    UITableViewCell subclass and I could get the code to draw the label into the cell and add the subview to work.  That was very straightforward.
  • On Jul 29, 2012, at 7:38 PM, Alex Zavatone <zav...> wrote:

    >
    > On Jul 29, 2012, at 10:30 PM, David Duncan wrote:
    >
    >> On Jul 29, 2012, at 7:02 PM, Alex Zavatone <zav...> wrote:
    >>
    >>> I've put together a TVC with a custom TVC cell class consisting mainly of UILabels, but have the problem where the labels will have varying amounts of lines of text.
    >>>
    >>> The main issue is that in iOS 5.x, UILabels still can't be vertically aligned to the top.
    >>>
    >>> There is a fair amount of source that explains how to add a label as a subView, but it is all non ARC code so, as cells get reused, the UILabel will get recreated whenever cellForRowAtIndexPath is called.
    >>>
    >>> Does anyone have ideas on when to remove the last subview of the content cell or how to check for presence of the view already so that It doesn't get to allocated again every time the cell displays in the cellForRowAtIndexPath code?
    >>
    >>
    >> ARC really doesn't have anything to do with this. But my question would be why are you adding new labels in cellForRowAtIndexPath when you've already created a subclass of UITableViewCell? Have your subclass create (and maintain a reference) to these labels and never recreate them again.
    >
    > Well, because I could not get setting the frame size of a storyboarded UILabel to do anything at all in the
    > UITableViewCell subclass and I could get the code to draw the label into the cell and add the subview to work.  That was very straightforward.

    Are you resizing the label inside of -layoutSubviews?
    --
    David Duncan
  • On Jul 29, 2012, at 10:48 PM, David Duncan wrote:

    > On Jul 29, 2012, at 7:38 PM, Alex Zavatone <zav...> wrote:
    >
    >>
    >> On Jul 29, 2012, at 10:30 PM, David Duncan wrote:
    >>
    >>> On Jul 29, 2012, at 7:02 PM, Alex Zavatone <zav...> wrote:
    >>>
    >>>> I've put together a TVC with a custom TVC cell class consisting mainly of UILabels, but have the problem where the labels will have varying amounts of lines of text.
    >>>>
    >>>> The main issue is that in iOS 5.x, UILabels still can't be vertically aligned to the top.
    >>>>
    >>>> There is a fair amount of source that explains how to add a label as a subView, but it is all non ARC code so, as cells get reused, the UILabel will get recreated whenever cellForRowAtIndexPath is called.
    >>>>
    >>>> Does anyone have ideas on when to remove the last subview of the content cell or how to check for presence of the view already so that It doesn't get to allocated again every time the cell displays in the cellForRowAtIndexPath code?
    >>>
    >>>
    >>> ARC really doesn't have anything to do with this. But my question would be why are you adding new labels in cellForRowAtIndexPath when you've already created a subclass of UITableViewCell? Have your subclass create (and maintain a reference) to these labels and never recreate them again.
    >>
    >> Well, because I could not get setting the frame size of a storyboarded UILabel to do anything at all in the
    >> UITableViewCell subclass and I could get the code to draw the label into the cell and add the subview to work.  That was very straightforward.
    >
    > Are you resizing the label inside of -layoutSubviews?

    In the TVC, I was doing all the operations in cellForRowAtIndexPath.

    That includes referencing all the UILabel IBOutlets from the TableViewCell subclass and assigning their values.

    The debugger (po) is very uncooperative with getting properties out of the cell.myUILabelVariable while in cellForRowAtIndexPath so I was taking the path of least resistance.
  • On Jul 29, 2012, at 7:55 PM, Alex Zavatone <zav...> wrote:

    >
    > On Jul 29, 2012, at 10:48 PM, David Duncan wrote:
    >
    >> On Jul 29, 2012, at 7:38 PM, Alex Zavatone <zav...> wrote:
    >>
    >>>
    >>> On Jul 29, 2012, at 10:30 PM, David Duncan wrote:
    >>>
    >>>> On Jul 29, 2012, at 7:02 PM, Alex Zavatone <zav...> wrote:
    >>>>
    >>>>> I've put together a TVC with a custom TVC cell class consisting mainly of UILabels, but have the problem where the labels will have varying amounts of lines of text.
    >>>>>
    >>>>> The main issue is that in iOS 5.x, UILabels still can't be vertically aligned to the top.
    >>>>>
    >>>>> There is a fair amount of source that explains how to add a label as a subView, but it is all non ARC code so, as cells get reused, the UILabel will get recreated whenever cellForRowAtIndexPath is called.
    >>>>>
    >>>>> Does anyone have ideas on when to remove the last subview of the content cell or how to check for presence of the view already so that It doesn't get to allocated again every time the cell displays in the cellForRowAtIndexPath code?
    >>>>
    >>>>
    >>>> ARC really doesn't have anything to do with this. But my question would be why are you adding new labels in cellForRowAtIndexPath when you've already created a subclass of UITableViewCell? Have your subclass create (and maintain a reference) to these labels and never recreate them again.
    >>>
    >>> Well, because I could not get setting the frame size of a storyboarded UILabel to do anything at all in the
    >>> UITableViewCell subclass and I could get the code to draw the label into the cell and add the subview to work.  That was very straightforward.
    >>
    >> Are you resizing the label inside of -layoutSubviews?
    >
    > In the TVC, I was doing all the operations in cellForRowAtIndexPath.
    >
    > That includes referencing all the UILabel IBOutlets from the TableViewCell subclass and assigning their values.

    So then how does this cause the issue you you mention above? Simply referencing the outlet isn't going to cause additional views to be added. Unless you are assigning a new label to the outlet (which is counter intuitive to the concept of an outlet in the first place) the only label you'll get is the one from the nib.

    But even that said, on shipping versions of iOS, the cell's size isn't necessarily set inside of cellForRowAtIndexPath:, so if you want to layout with respect to the size of the cell you still aren't going to have that information at this point.

    --
    David Duncan
  • Thanks. I moved the resizing of the frame in a method in the custom cell class instead of in the TVC's cellForRowAtIndexPath.

    What is strange is that I call it from the TVC's cellForRowAtIndexPath and it works just fine, but didn't do anything when in the TVC's cellForRowAtIndexPath.

    Not exactly comforting, but it works and I don't need to resort to a UITextView.  Thanks again.

    On Jul 29, 2012, at 10:48 PM, David Duncan wrote:

    > On Jul 29, 2012, at 7:38 PM, Alex Zavatone <zav...> wrote:
    >
    >>
    >> On Jul 29, 2012, at 10:30 PM, David Duncan wrote:
    >>
    >>> On Jul 29, 2012, at 7:02 PM, Alex Zavatone <zav...> wrote:
    >>>
    >>>> I've put together a TVC with a custom TVC cell class consisting mainly of UILabels, but have the problem where the labels will have varying amounts of lines of text.
    >>>>
    >>>> The main issue is that in iOS 5.x, UILabels still can't be vertically aligned to the top.
    >>>>
    >>>> There is a fair amount of source that explains how to add a label as a subView, but it is all non ARC code so, as cells get reused, the UILabel will get recreated whenever cellForRowAtIndexPath is called.
    >>>>
    >>>> Does anyone have ideas on when to remove the last subview of the content cell or how to check for presence of the view already so that It doesn't get to allocated again every time the cell displays in the cellForRowAtIndexPath code?
    >>>
    >>>
    >>> ARC really doesn't have anything to do with this. But my question would be why are you adding new labels in cellForRowAtIndexPath when you've already created a subclass of UITableViewCell? Have your subclass create (and maintain a reference) to these labels and never recreate them again.
    >>
    >> Well, because I could not get setting the frame size of a storyboarded UILabel to do anything at all in the
    >> UITableViewCell subclass and I could get the code to draw the label into the cell and add the subview to work.  That was very straightforward.
    >
    > Are you resizing the label inside of -layoutSubviews?
    > --
    > David Duncan
    >
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