Question

I have a problem reloading a custom UIView (SBBarChart) inside of a table cell. I don't use autolayout because it was too difficult to adjust it will all components of the cell which may be there or not. I manually calculate each time the rectangles and that works.

In respect to the rotation, I have tried implementing in the table view controller the willRotateToInterfaceOrientation method with [self.tableView reloadData] inside. This generates the cells again after rotation. Fine.

Inside of the cell class, I invoke [self.barChart setNeedsDisplay] to ensure that the subviews of the chart will be reloaded, which works.

Now the problem. Initially, portrait view opens and loads correctly. When I rotate to landscape, that works as well. When I rotate back to portrait, the length of the bar is calculated correctly but still displayed as it was in landscape. So the new rectangle for the bar has the right measures but the bar is displayed too long and partly outside of the screen.

A label with the same measures for the frame is displayed (and centered) correctly, so it should be a problem of the bar view reloading.

What am I doing wrong?

Était-ce utile?

La solution 2

I just solved my problem. I found three major mistakes in my code.

1) I was using dequeueReusableCellWithIdentifier after removing controls from cells. This resulted in missing controls and strange behavior. Now I generate each time a new cell from the nib.

2) I was using x and y values of controls which I had removed from the cell. I just put the numeric values instead.

3) I was using the frame for determining the width of the screen. However, the frame does not change. To solve this, I used the following thread: How to get orientation-dependent height and width of the screen?

Now, I don't even need the line [self.barChart setNeedsDisplay] and I can rotate the device as much as I want, the controls don't shift anymore. :)

I am still not sure that I have to invoke reloadData for adjusting the layout but I haven't found a better solution.

So, finally it works! Thank you a lot for the help and I hope, this post will help someone else. :)

Autres conseils

Try checking the [[UIApplication sharedApplication] statusBarOrientation]

when reloading your tableview then adjust accordingly. That will give the current rotation status.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top