Turn custom UITableViewCell

I have a custom UITableViewCell that contains several UIButtons. The position of each button depends on the width of the cell. I set autoresizingMask = UIViewAutoresizingFlexibleWidth so that it correctly adjusts the cell width and button position when the application is launched from the device in landscape or portrait mode.

The problem is that the device rotates from one mode to another, the buttons do not adjust the position, because the UITableViewCell is reusable. In other words, the cell is not initialized based on the new UITalbeView width, because the cell function initWithStyle is called before the device is rotated and is not called again after the device is rotated. Any suggestions?

+7
iphone uitableview rotation reusability
source share
6 answers

After spending hours of research (including posts on this site), I could not find any solutions. But the light suddenly turns on. The solution is very simple. Just determine if the device orientation is landscape or portrait mode, and define a different name for each ReusableCellIdentifier.

static NSString*Identifier; if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft && [UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight) { Identifier= @"aCell_portrait"; } else Identifier= @"DocumentOptionIdentifier_Landscape"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 
+7
source share

Since UITableViewCell is also a UIView, you can override the setFrame method. Each time your table scan rotates, this method will be called for all cells.

 -(void)setFrame:(CGRect)frame { [super setFrame:frame]; //Do your rotation stuffs here :) } 
+13
source share

The previous answer has a serious problem. You should use [UIApplication sharedApplication] .statusBarOrientation instead of [UIDevice currebtDevice]. Orientation, because the orientation of the device has nothing to do with the orientation of the interface. The orientation of the device is physical rotation based on the accelerometer.

+6
source share

The marked answer works like a charm in older versions of iOS. For iOS 6.0, I used the following code:

 static NSString *Identifier; if (self.interfaceOrientation==UIInterfaceOrientationPortrait) { Identifier=@"aCell_portrait"; } else { Identifier=@"DocumentOptionIdentifier_Landscape"; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 
+2
source share

You will need to fix the cell border width (provided that the height is the same in portrait and landscape modes) in your cellForRowAtIndexPath method. Here is what works here. I used to create a custom TableViewCell with IB, and it is always initialized for a portrait width of 320 pixels. When defining a frame, it works as expected, even if the cell is "reused" from the queue.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { // create cell here... } // Adjust cell frame width to be equal to tableview frame width cell.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.frame.size.height); ... } 
+1
source share

I had a similar problem and this post helped me. In my case, I have a custom class declared in a separate file, and in this file I have the following code in layoutSubviews :

 //PORTRAIT CELL if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft && [UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight) { //build the custom content views for portrait mode here } else { //build the custom content views for landscape mode here } 

Then in my view of the controller, I simply implement willAnimateRotationToInterfaceOrientation: and send the message reloadData to my table view.

In this case, I do not need to touch the cellForRow method.

0
source share

All Articles