Rotate a custom UITableViewCell

前端 未结 6 504
有刺的猬
有刺的猬 2020-12-29 14:36

I have a custom UITableViewCell which contains several UIButtons. Each button\'s frame position is relative to the cell width. I set autoresizingMask=UIViewAutoresizingFlexi

相关标签:
6条回答
  • 2020-12-29 15:07

    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);
     ...
    }
    
    0 讨论(0)
  • 2020-12-29 15:08

    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 :)
    } 
    
    0 讨论(0)
  • 2020-12-29 15:09

    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];
    
    0 讨论(0)
  • 2020-12-29 15:16

    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.

    0 讨论(0)
  • 2020-12-29 15:17

    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];
    
    0 讨论(0)
  • 2020-12-29 15:20

    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.

    0 讨论(0)
提交回复
热议问题