views:

558

answers:

5

I want to resize my table view cells from inside the cell instead of from the UITableViewDelegate. I am resizing them based on asynchronous content, so I can't size them in the delegate.

I tried setting self.frame inside the cell, but the table view was really unhappy about that. Cells were overlapping and all kinds of craziness was going on.

A: 

You should try to manage the cells contentView propertys frame, instead of the cells frame itself heres a reference http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell%5FClass/Reference/Reference.html#//apple%5Fref/occ/instp/UITableViewCell/contentView

Daniel
I am adding views in the contentView. I am resizing `self.frame` instead of `self.contentView.frame` because the contentView auto-resizes with the autoResizingMask.Either way, setting the frame of `self.view` or `self.contentView` doesn't fix the issue. The tableView still is super confused if you size it from inside the cell.
Sam Soffes
+4  A: 

You simply have to use the table view to control height. You can tell the table a cell has altered by using the calls to remove and then re-add specific cells, so you don't have to reload the whole table - but the height has to be fetched using the delegate callback tableView:heightForRow:atIndexPath:

I don't see why this is not practical though. You can have any number of asynch systems running that update a central height cache held by the table view delegate - every time you create a cell you can assign it the delegate as a reference so it has a way to talk back to the table and let it know cells need reloading and what the new heights are.

If you think about it, the poor table view is a scroll view that has to manage all these separate cells and keep them together visually - so it's really unkind of a cell to go rogue and start altering frames without letting the table view know what is going on anyway. It's best to let the table drive and tell it what to do.

Kendall Helmstetter Gelner
+1  A: 

No you can not set the cell's size without using the UITableViewDelegate. Changing the size of the cell with actually change the size of the cell, but it will not change the offsets that the UITableView draws the cells with. Which will result in overlaps, and gaps all over the place.

Your friend is tableView:heightForRowAtIndexPath:, and it should be fast. If you override it, then the table view can no longer make the assumption that all rows are of the same height. And thus it must query all rows for their height each time it fetches cells to draw.

PeyloW
A: 

peyloW, do you have and example code?

jogo bonito
This is not an answer to the question. Use the comment feature instead.
Shaggy Frog
A: 

dude.. this might help.. it did for me.. http://primates.ximian.com/~miguel/tmp/monocatalog/alert.cs

nish
C# code doesn't help anyone with iPhone dev.
Sam Soffes