PDA

View Full Version : Complex tableview cell -like facebook




mraheel
Aug 13, 2010, 03:53 PM
Hey,

I've got a requirement for something that if i used uiwebview in each cell, it would make life hell-u-va lot easier.. but then, its not recommended .. at all!

So, i decided to create a custom cell.

Lets consider 4 controls in a cell. One UIImageView, 2 UILabels and 1 UIButton.
the UIImageView position can be static in the cell predefined (at the left corner). No problems here.


UIButton (sporting an image) and UILabel come next in terms of horizontal rect.x and Beyond that, UILabel2. X= (15+uilabel.bounds.size.width) something like that.

Heres what I want,
What If UILabel1 could not get any string (empty) from the data source, In that event, I want the cell to realign itself. The UILabel2 pushes (starts where UILabel1 would start if existed) back and uilabel1 vanishes.
Similarly, if UIImageView has no image from datasource, Both the labels move a few .x behind

I think you see where this is going. Three20 probably has this built in. But I feel as if I lost control if I use it, besides, I wanted to learn this:

I think layout subviews is where the fuss!. Its the logic proceedure thats a little confusing.

Any direction is much appreciated.
thanks



PhoneyDeveloper
Aug 13, 2010, 04:44 PM
layoutSubviews is the place to put this logic. It's straightforward although your code may be a little complex. What makes it harder is supporting portrait, landscape, iPad.

Do your logic relative to the contentView. Call super, then get the contentView's bounds and then make your adjustments to the positions of the subviews. I've never tried to add or remove subviews in layoutSubviews. I think that if a label is empty then it doesn't matter if it's just sitting there behind another view.

I can't tell if your cell is fixed height or not. If not then your view controller also has to be able to figure out how high the cell will be, which is another pain.

mraheel
Aug 13, 2010, 05:23 PM
Okay, I probably need to check if the Label is empty in Layoutsubviews? if yes, move the other label back? Is it called before filling up the cell??

And Yea, I do need to resize the cell height. Obviously, it mite be a pain computing the cell height of both labels and the larger one is equal to the cell height.

PhoneyDeveloper
Aug 14, 2010, 08:53 AM
Yes, you can check the content of the labels in layoutSubviews and size the labels accordingly and position them as well.

layoutSubviews will be called somewhere between cellForRowAtIndexPath and the appearance of the cell onscreen. You can force it to be called by sending setNeedsLayout in cellForRowAtIndexPath but I don't think you'll need to.

Since you'll need to implement heightForRowAtIndexPath you'll need to duplicate some of the logic there as in layoutSubviews. In theory you could add a class method to your cell that returns the height given particular inputs, but I haven't done it that way.