UITableView Did Load

Question

How can you determine when a table view is performed, drawing cells?

Problem

I got two labels inside the contentView of a UITableViewCell. The size of these labels is dynamic. I was able to do this by subclassing the UITableViewCell, in the drawRect method I adjust the frames of two labels depending on their contents. Now I want to align all other marks.

My thoughts are steps away

  • Define the content in the table view and load it automatically.
  • Go through the table view cells and determine the x position of the second label in the UITableViewCell, which is the farthest.
  • Save 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, which means that I cannot determine the size of the frame and, therefore, cannot find the correct x position to align all the second labels.

I tried to subclass UITableViewController and look at events such as viewDidLoad and viewDidAppear, unfortunately, the cells are not drawn in these events yet.

What I want...

I want the table view to draw cells at least once so that I can define label sizes inside the table view cell. I decided to do this by going through all the cells using cellForRow, but although it successfully returns the cell, the content is not drawn, but that means the frame remains at zero width.

Anyone have a solution?

Thanks in advance,

Mark

+6
iphone uitableview drawrect
source share
3 answers

Try calling sizeWithFont: contents of these labels to get the maximum width before drawing anything. You can use it later in cellForRowAtIndexPath: to adjust the width as you need.

I would recommend that you reconsider the use of UITableViewCellStyleValue2 cells instead and try setting up textLabel and detailTextLabel. I had a similar situation, and this is how I did it.

+2
source share

Firstly, you really just need to choose the explicit fixed position at which the first label ends, and the second starts based on what you know about the minimum and maximum length of the text that will be placed in these labels, This will completely eliminate this problem.

But if you want a solution: use the sizeWithFont: method or one of its cousins ​​(see Xcode docs). In particular, scroll through the values ​​that appear in the first shortcuts, apply sizeWithFont to them sizeWithFont and track the largest width you see. (I assume that you have access to the values ​​before they enter the cells, since they are dynamic, they must go through the table view controller, no?)

Now you have the value you are looking for, without having to do extremely wasteful work of creating a bunch of cell objects and never use them for their intended purpose.

+1
source share

I think what you need to do is to add the viewController in , so that the UITableViewController controls the UITableViewCell itself, so that you can capture label load events. The viewController will have links to both labels so that it can adjust them accordingly in response to -viewDidAppear .

I have never done this, but a UITableViewCell is a view like any other, so you should be able to configure a controller for it. You may need to manually activate the controller, because in this context you do not have a navigation controller to do this for you.

0
source share

All Articles