UITableView Did Load (Done drawing the cells)

Question

How can you detect when the Table View is done drawing the cells?

Issue

I got two labels within the contentView of an UITableViewCell. The size of these labels are dynamic. I was able to do so by subclassing UITableViewCell, in the drawRect method I adjust the frames of the two labels depending on their content. Now I want to align all the second labels.

My Thoughts in Steps
  1. Determine the content in the table view and let it load automatically.
  2. Run through the table view cells and determine the x position of the second label within the UITableViewCell that is the furtherest away.
  3. Store this x position and when any cell is drawn use this x position to place the second label.

The problem is that if I use the following code:

for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) {
    UITableViewCustomCell *cell = (UITableViewCustomCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
    NSLog ([cell.labelTwo description]);        
}

The second label has not yet been drawn, meaning I can't determine the size of the frame and thus can not find the proper x position to align all second labels.

I have tried subclassing the UITableViewController and looking at events such as viewDidLoad and viewDidAppear unfortunatly also in these events the cells aren't drawn yet.

What I Want ...

What I want is for the table view to draw the cells at least once so I can determine the sizes of the labels within the table view cell. I thought to accomplish this by looping through all the cells with cellForRow, but although it successfully returns the cell the content is not drawn yet meaning the frame remains with a width of zero.

Does anyone have a solution?

Thanks in advance,

Mark

Answers


Try calling sizeWithFont: on the contents of these labels to get the max width before you draw anything. You should be able to use it later in your cellForRowAtIndexPath: to adjust the width as you need.

I would recommend you reconsider using UITableViewCellStyleValue2 cells instead and attempt to configure the textLabel and detailTextLabel. I had a similar situation and this is how I did it.


Need Your Help

Jquery $(element).hide().blur().show() showing up all elements

javascript jquery html

i have 3 drop down list on my page.2 have display set to none.When i click on drop down list(note just click, do not select) and mouse hover on the hello text the first drop down is reset but the d...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.