Re: Efficient UITableViewCell usage
Re: Efficient UITableViewCell usage
- Subject: Re: Efficient UITableViewCell usage
- From: Steve Christensen <email@hidden>
- Date: Mon, 30 Sep 2013 08:15:23 -0700
The table view needs to call -heightForRowAtIndexPath: for each cell in the table, at the very least, so that it knows how tall the table is so that it can set -contentSize on it's underlying scroll view.
For performance, you could calculate and cache the cell heights in an array managed by your view controller, then just return the cached values. If you calculate them in your view controller's -viewWillLayoutSubviews method then you can adjust the heights when the screen rotation changes.
Sent from my iPhone
> On Sep 30, 2013, at 7:47 AM, Koen van der Drift <email@hidden> wrote:
>
> Hi,
>
> I have a custom UITableViewCell with several labels of which the size needs to be calculated based on the content. Some of the content is also constructed on the fly, since I am generating an attributed string, and don't want to store these in my model objects.
>
> The frame is calculated in heightForRowAtIndexPath: and layoutSubviews of my custom cell. The attributed string is calculated in heightForRowAtIndexPath: and cellForRowAtIndexPath:
>
> I put in some logs to see the order in which these methods are called, see below. Surprisingly (at least for me), heightForRowAtIndexPath is called for every row first, even the ones that are not yet visible. So when my data array is really large, this could take up some time, and I can see that when the table is drawn.
>
> There must be a more clever way to do this?
>
> - Koen.
>
>
>
> 2013-09-30 09:50:30.674 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.675 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.676 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.677 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.677 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.678 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.679 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.680 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.680 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.681 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.682 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.683 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.683 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.684 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.685 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.686 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.686 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.687 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.688 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.688 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.689 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.690 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.691 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.691 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.692 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.693 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.693 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.694 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.695 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.696 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.697 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.697 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.698 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.699 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.700 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.701 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.701 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.702 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.703 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.704 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.704 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.705 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.706 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.707 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.707 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.708 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.709 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.709 MyApp[23200:a0b] heightForRowAtIndexPath
> 2013-09-30 09:50:30.727 MyApp[23200:a0b] cellForRowAtIndexPath
> 2013-09-30 09:50:30.729 MyApp[23200:a0b] cellForRowAtIndexPath
> 2013-09-30 09:50:30.731 MyApp[23200:a0b] cellForRowAtIndexPath
> 2013-09-30 09:50:30.732 MyApp[23200:a0b] cellForRowAtIndexPath
> 2013-09-30 09:50:30.734 MyApp[23200:a0b] cellForRowAtIndexPath
> 2013-09-30 09:50:30.735 MyApp[23200:a0b] cellForRowAtIndexPath
> 2013-09-30 09:50:30.737 MyApp[23200:a0b] cellForRowAtIndexPath
> 2013-09-30 09:50:30.738 MyApp[23200:a0b] layoutsubviews
> 2013-09-30 09:50:30.739 MyApp[23200:a0b] layoutsubviews
> 2013-09-30 09:50:30.740 MyApp[23200:a0b] layoutsubviews
> 2013-09-30 09:50:30.741 MyApp[23200:a0b] layoutsubviews
> 2013-09-30 09:50:30.741 MyApp[23200:a0b] layoutsubviews
> 2013-09-30 09:50:30.742 MyApp[23200:a0b] layoutsubviews
> 2013-09-30 09:50:30.743 MyApp[23200:a0b] layoutsubviews
> 2013-09-30 09:50:30.754 MyApp[23200:a0b] layoutsubviews
>
>
> _______________________________________________
>
> 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
_______________________________________________
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