Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 742
Mark Avatar asked Jan 26 '10 14:01

Mark


1 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.

like image 200
Ahmed Abdelkader Avatar answered Oct 09 '22 12:10

Ahmed Abdelkader