Rotate a custom UITableViewCell

房东的猫 提交于 2019-11-30 05:19:49

Since UITableViewCell is also a UIView, you can override setFrame method. Everytime that your table view rotates, this method will be called for all cells.

-(void)setFrame:(CGRect)frame
{
    [super setFrame:frame];

    //Do your rotation stuffs here :)
} 

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

static NSString*Identifier;

if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft && [UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight) {
                Identifier= @"aCell_portrait";
            }
            else Identifier= @"DocumentOptionIdentifier_Landscape";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];

Previous answer has a serious problem. You should use [UIApplication sharedApplication].statusBarOrientation instead of [UIDevice currebtDevice].orientation because Device orientation has nothing to do with interface orientation - Device orientation is physical rotation based on accelerometer.

The ticked answer works like a charm in old iOS versions. For iOS 6.0 I used the next code:

static NSString *Identifier;
if (self.interfaceOrientation==UIInterfaceOrientationPortrait) {
    Identifier=@"aCell_portrait";
}
else {
    Identifier=@"DocumentOptionIdentifier_Landscape";
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];

You'll need to fix your cells frame width (assuming the height is same in portrait and landscape mode) within your cellForRowAtIndexPath method. That is what is working here. I used to create a custom TableViewCell with IB and it is always initialised for portrait 320 px width. With defining the frame it works as expected, even if the cell is "reused" from 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);
 ...
}
Giovanni

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 controller I just implement willAnimateRotationToInterfaceOrientation: and send the reloadData message to my table view.

With this I don't have to touch the cellForRow method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!