UA-17470720-3

Jump to content


Photo
- - - - -

access object frame after autoresizing occurs in custom cell

cells

  • Please log in to reply
6 replies to this topic

#1 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 03 March 2013 - 06:57 AM

HI everyone,

I have a custom cell I'm using to display a variable length subject and body - like a blog post. The cell is pretty basic: two UILables stacked one atop the the other.

The Issue I have is that I want to keep a consistent distance between the two labels regardless of how "tall" they grow to when STV autosizes them. So if the title is one line long or three lines long, I want the summary text to start just a bit below it and not have a wide gap on shorter titles.

I thought I could calculate the size and frame of the title and then adjust the summary label by getting the y coordinate + height + padding, but it doesn't seem to work because it does not appear I am getting the frames after autoresizing has adjusted them.

I realize that this is something that auto layout would handle nicely, however I'm supporting IOS 5 in this project.

So here is my basic custom cell:

SCCustomCell *customCell = [SCCustomCell cellWithText:nil
objectBindingsString:@"1:title;2:summaryTxt;3:articleDate" nibName:@"CustomArticleCell"];

I am using this in an arrayOfObjectsSection and returning it in sectionActions.cellForRowAtIndexPath ...


I tried to pull the UILabel back out of the custom cell, evaluate their frames height and then adjust them. I can adjust them, but the frame sizes appear to be the same as what they are in the nib so any calculations don't seem to work.


UILabel *titleLabel = (UILabel*)[customCell viewWithTag:1]

UILabel *sumLabel = (UILabel*)[customCell viewWithTag:2];

CGRect tmpframe = sumLabel.frame;
tmpframe.origin.y = titleLabel.frame.origin.y + titleLabel.frame.size.height + 5;
sumLabel.frame = tmpframe;

So, is this the correct way to go about this? Is this the correct place to do it? Any other suggestions would be help!

note: I've started to try recalculating in cellActions.will [configure,style,display]. I'm hoping that is the correct place to make the adjustment.


thanks so much!

Edited by Everett, 03 March 2013 - 07:10 AM.


#2 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 03 March 2013 - 11:17 AM

Ok, got this working pretty well - still one little question I can figure out.

kudos out to Tommy Wild for an older post that helped me tremendously but I didn't catch on my first search.
I wanted to let STV do it's autosizing thing, in in cellActions.willConfigure, calculated the future height of the title and then moved the frame of the summary to be in the correct place. Later when that cell get's fully configured and autosized, the layout will be correct.

Here is the code I used to modify the layout in cellAction.willConfigure


if ([cell isKindOfClass:[SCCustomCell class]]) {


SCCustomCell *customCell = (SCCustomCell *)cell;

UILabel *titleLabel = (UILabel*)[customCell viewWithTag:1];

UILabel *summaryLabel = (UILabel*)[customCell viewWithTag:2];


Article *a = [self.parsedItems objectAtIndex:indexPath.row];

CGSize titleSize = [a.title sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(titleLabel.frame.size.width, 9999) lineBreakMode:UILineBreakModeWordWrap];


//determine the layout to see if cell height should change

CGRect tempframe = summaryLabel.frame;

tempframe.origin.y = titleLabel.frame.origin.y + titleSize.height + 5;

summaryLabel.frame = tempframe;


}


If I could use auto layout and let stv autosize, it would be even simpler.
be well!

#3 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 03 March 2013 - 08:04 PM

Hi everyone,
I'm still having a periodic issue where the summary label may get adjusted where it extends beyond the bottom of the cell. Any thoughts on the best way to accomplish this and were is the best place to try and adjust the cell height after stv resizes the labels?
thanks in advance!!!

#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 04 March 2013 - 03:04 PM

Hi Everett,

As per our discussion in the chat room, the best way to go about this is to prevent STV from doing any automatic calculations and directly provide the height yourself. You can easily do that by implementing sectionActions.customHeightForRowAtIndexPath . Please let me know if that works for you.

#5 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 11 March 2013 - 08:15 PM

Hi Tarek,
I finally had some time tonight so I tried out your suggestion and it worked nicely. I had to work out a couple of issues so I thought I'd post them in case there is a better way or maybe they will help someone else.

First, I wasn't sure if you wanted me to use only the sectionActions.customHeightForRowAtIndexPath in lieu of cellActions.willConfigure. I tried using just the sectionActions, but I could not get it to actually layout my cell, so instead I implemented both methods: cellActions.willConfigure to move the labels around in the cell and to calculate the height, and sectionActions to actually set the height.

I use this approach in cellActions.willConfigure to calculate the height of the title label and adjust the summary label as needed:

if ([cell isKindOfClass:[SCCustomCell class]]) {

SCCustomCell *customCell = (SCCustomCell *)cell;
UILabel *titleLabel = (UILabel*)[customCell viewWithTag:1];
UILabel *summaryLabel = (UILabel*)[customCell viewWithTag:2];

Article *a = (Article*)cell.boundObject;
CGSize titleSize = [a.title sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(titleLabel.frame.size.width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

CGSize bodySize = [a.summaryTxt sizeWithFont:summaryLabel.font constrainedToSize:CGSizeMake(titleLabel.frame.size.width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

//dynamically layout summary label 5pts below title label
//title text can be between 1 and 3 lines
CGRect summaryframe = summaryLabel.frame;
summaryframe.origin.y = titleLabel.frame.origin.y + titleSize.height + 5;
summaryLabel.frame = summaryframe;

//calculate cell height
//summaryLabel.y-value + its calculated height + padding
//store this as the tag value for easy recall
cell.tag = summaryLabel.frame.origin.y + bodySize.height + 10;
}

Note: I'm calculating what the total cell height should be and storing it in cell.tag for easy recall. I pull that out later as I'll explain.

in sectionActions.customHeightForRowAtIndexPath, the first thing that there is no reference to the cell, but since there is a reference to the items section, we can get the cell. Here is the method:


articleList.sectionActions.customHeightForRowAtIndexPath = ^CGFloat(SCArrayOfItemsSection *itemsSection, NSIndexPath *indexPath)
{
SCTableViewCell *cell = [itemsSection.ownerTableViewModel cellAtIndexPath:indexPath];
return cell.tag; //Note:I'll likely create a subclass to hold this as a property
};

Because all the work was done in the cellActions.willConfigure, this method was very basic and just returns the stored value for height in cell.tag.

Is this an acceptable approach? Any tips to optimize? My text alignment is now flawless.

thanks,
Ev

Edited by Everett, 12 March 2013 - 03:23 AM.

  • Tarek and wizgod like this

#6 cometlinear

cometlinear

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 55 posts
  • LocationCalifornia
Reputation: 10
Good

Posted 12 March 2013 - 05:49 PM

Just FYI, sometimes this sort of thing can be achieved by using a transparent GIF or PNG graphic.
  • Tarek likes this

#7 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 13 March 2013 - 11:26 AM

Thank you very much Everett :)





Also tagged with one or more of these keywords: cells

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users