Still isn't working for me. Could you post your entire code for the
cellForRowAtIndexPath delegate method?
Thanks
On May 4, 1:11 pm, Rich MacDonald <
rich.macdon...@gmail.com> wrote:
> Chris, thanks for your feedback. I don't know if I would have caught this
> if you hadn't mentioned sizeToFit working but that you hadn't implemented
> cell reuse yet. Your last update made it even more clear that reuse was the
> culprit.
> I've gotten sizeToFit to work now by not bothering to set the label frame
> when first creating it and instead setting the frame with appropriate width
> and arbitrary height just before calling size to fit:
>
> statusLabel.frame = CGRectMake(CELL_PADDING, CELL_PADDING, statusLabelWidth,
> 20);
>
> [statusLabel sizeToFit];
>
> Where statusLabelWidth was calculated as: tableView.bounds.size.width - 4*
> CELL_PADDING; with #define CELL_PADDING 10
>
> Works like a charm on all the users tweets. So it seems that resizeToFit
> would, understandably, change the frame width when it encountered short
> one-liners like SHAQ's "@tgoace got u" or "@jromulo great point". Then when
> the cell was queued and subsequently dequeued the width remained at this
> reduced size, leading to our wrapping issues with too many lines.
>
> This also explains why saw no issues with Woz. The only one-liner was at
> the bottom of the table view. This made me think that a good final test to
> confirm this theory would be to scroll fully back up and down repeatedly to
> see if eventually a cell's rendering would get messed up when the resized
> width cell made it through the queue. Took many repetitions, but eventually
> it did mess up the top cell:
>
> [image: Woz Messed Up.png]
>
> Here's a composited image where the messed up row is put just above the
> last, one-liner row row with an added guide to compare their widths:
>
> [image: Woz Comp.png]
>
> From Table View Programming Guide for iPhone OS: A Closer Look at Table-View
> Cells<
http://developer.apple.com/iphone/library/documentation/UserExperienc...>
> :
>
> The table view’s data source implementation of
> tableView:cellForRowAtIndexPath:<
http://developer.apple.com/iphone/library/documentation/UIKit/Referen...>
> should *always* reset all content when reusing a cell.
>
> After dealing with this confusing behavior, I now understand why they
> emphasize this point; it's easy to think the content properties haven't
> changed, but it's easy to overlook state-change side effects.
>
> -Rich
>
>
>
> On Mon, May 4, 2009 at 10:05 AM, Chris Schepman <
csc...@gmail.com> wrote:
> > When I enable cell reuse the same thing happens to me. The widths go
> > shorter and the text runs off the screen. It doesn't happen for Woz as well.
> > Weird.
>
> > So far I've been getting this stuff no problem, but I've spent a few days
> > on this problem. I can't wait to understand it.
>
> > There must be way to get the width set correctly, but I'm bummed that
> > -sizeToFit doesn't handle it. ;)
>
> > On Mon, May 4, 2009 at 1:47 AM, Rich MacDonald <
rich.macdon...@gmail.com>wrote:
>
> >> Interesting. Someone else suggested sizeToFit, and it sounded like their
> >> method relied on calculating the row height by duplicating the label setup
> >> code in there (i.e. As opposed to the NSString code from class).
>
> >> For whatever reason sizeToFit works 90% of the time for me, but with SHAQ
> >> and Shatner, a few label widths get shortened so that the wrapping results
> >> in more lines of text than the row height budgeted for. Maybe in the act of
> >> reusing cells the width gets shortened on one or two of the cells and I need
> >> to reset it outside the cell creation code so it will work after reuse...
>
> >> -Rich
>
> >> On May 4, 2009, at 12:27 AM, Chris Schepman <
csc...@gmail.com> wrote:
>
> >> I just realized that ...
>
> >> statusLabel.text = [[person statuses] objectAtIndex:indexPath.row];
>
> >> [statusLabel sizeToFit];
>
> >> seems to work without having to make the calculation again. I suppose it's
> >> making it for us...still tinkering with the cell reuse though.
>
> >> On Sun, May 3, 2009 at 10:14 PM, Rich MacDonald <<
rich.macdon...@gmail.com>
> >>
rich.macdon...@gmail.com> wrote:
>
> >>> I haven't figured out - or read in these threads - any solutions that
> >>> don't involve making this height calculation twice or at least caching the
> >>> height in an array for quicker look-up the second time. This does seem a
> >>> bit inefficient/inelegant, and I too would love to hear if there is a better
> >>> solution.
>
> >>> In regards to the tableView:cellForRowAtIndexPath:, we're creating the
> >>> UITableViewCell ourselves so I hadn't put much stake in getting a useful
> >>> height from that (i.e. what we calculated from
> >>> tableView:heightForRowAtIndexPath: ). The dequeueReusableCellWithIdentifier:
> >>> UITableView method sounded like it might set the height for us, but it
> >>> seems like it's just returning a cell with the values when it was added to
> >>> the queue...definitely getting different heights than just 44 after
> >>> scrolling back through the table.
>
> >>> One possibility is to use
>
> >>> - (void)tableView:(UITableView *)tableView
> >>> willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
> >>> *)indexPath
>
> >>> which is a delegate method called after tableView:cellForRowAtIndexPath:
> >>> The cell passed here does indeed have the correct height for its frame, so
> >>> you could alter the label height backwards from that. But after doing this
> >>> successfully, I'm not sure I like the solution for two reasons:
>
> >>> 1. It requires a similar amount of code to work backwards from the
> >>> row height. This seems no better than duplicating row height calculation
> >>> code; and it is worse in that duplicating the code verbatim is only one
> >>> thing to figure out instead of two.
> >>> 2. It sounds like this delegate isn't intended for this kind of
> >>> alteration (from Table View Programming Guide):
>
> >>> The table view’s data source implementation of
> >>> tableView:cellForRowAtIndexPath: should *always* reset all content when
> >>> reusing a cell. In addition, a table view sends a
> >>> tableView:willDisplayCell:forRowAtIndexPath: message to its delegate
> >>> just before it draws a row. If the delegate chooses to implement this
> >>> method, it can customize the cell object before it is displayed. Note that
> >>> the delegate in this method should change state-based properties set earlier
> >>> by the table view, such as selection and background color, and not content.
>
> >>> So in the end, calculating the height once and caching it for later use
> >>> may be the most efficient route. Just have to make sure to invalidate the
> >>> cache if needed (e.g. rotation change causing cell width resizing).
>
> >>> -Rich
>
> >>> On Sun, May 3, 2009 at 4:42 PM, Chris Schepman < <
csc...@gmail.com>
> >>>
csc...@gmail.com> wrote:
>
> >>>> I'm struggling with this right now as well. It's like you have to
> >>>> calculate the display height for the cell. Then you have to calculate the
> >>>> same thing inside cellForRowAtIndexPath and set the contentView to the right
> >>>> size as well. I don't think the contentView is being sized with the cell...
>
> >>>> I'm not sure the cell is being sized either actually...it keeps coming
> >>>> in at a height of 44, even thought the heightForRowAtIndexPath is making the
> >>>> cells visibly much larger.
>
> >>>> hmm...I'm going to keep tinkering and I'll post if I find something that
> >>>> works. :)
>
> Woz Comp.png
> 23KViewDownload