1

UITableViewCell在通用应用程序中有一个自定义,我必须为 iPhone 和 iPad 设置不同的框架,我必须考虑不同的方向,但无法识别设备的方向。UIDeviceOrientation是在错误的地方?UIDeviceOrientation返回0。这是中的代码CustomCell.m

 -(void) layoutCellPortrait{
 if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

         //......frame subviews iPhone

 }else{

      //........frame subviews Ipad

 }

 }
 -(void) layoutCellLandscape{
     if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

         //.....frame subviews iPhone

     }else{

        //.......frame subviews Ipad

 }

}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
   {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]orientation];
    NSLog(@"The orientation is è %d", deviceOrientation);//here return 0

    if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {

         [self layoutCellPortrait];

    }
    else if (deviceOrientation== UIInterfaceOrientationLandscapeRight){
        [self layoutCellLandscape];

    }
    else if (deviceOrientation==UIInterfaceOrientationLandscapeLeft){

        [self layoutCellLandscape];

    }


    [self.contentView addSubview:self.subviews];

}
return self;
 }
4

5 回答 5

2

在你的类中实现旋转委托,每当发生旋转时,调用单元类中的一个方法,并以此方向作为输入参数(假设你正在调用reloadData桌子上的 a ,它反过来又调用方法中的这个cellForRowAtIndexpath方法。这应该重置框架相应地在每次旋转。

更新:

在轮值代表中,

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
  self.interfaceOrientation = orientation;
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
  [self.tableView reloadData];
}

在 tabelViewcellForRowAtIndexPath方法中,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *cellIdentifier = @"cell";
   CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   if (cell == nil) {
     cell = [[[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
   }
  //code..
  [cell rearrangeSubviewsForOrientation:self.interfaceOrientation];

  return cell;
}

在你的cell课堂上添加这个,

- (void)rearrangeSubviewsForOrientation:(UIInterfaceOrientation)orientation {
    if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

        //iPhone
        if(orientation == UIInterfaceOrientationPortrait || orientation== UIInterfaceOrientationPortraitUpsideDown) {
            //portait
        } else {
            //landscape
        }

    } else {

        //iPad
        if(orientation == UIInterfaceOrientationPortrait || orientation== UIInterfaceOrientationPortraitUpsideDown) {
            //portait
        } else {
            //landscape
        }
    }
}
于 2012-10-23T08:21:27.670 回答
2

尝试这个

-(void)layoutSubviews{
 --- your code ----
}
  1. 每次旋转设备时都会调用此方法。
  2. 在设备旋转时在要运行的括号内编写代码

注意:这是 UITableViewCell 的子类的类

如果您想检测类中的方向变化,该类是 UIViewController 之类的子类,下面有一个类似的方法,称为viewDidLayoutSubviews

- (void)viewDidLayoutSubviews{
 --- your code ---
} 
于 2016-05-25T11:45:28.997 回答
2

对于Swift,如果您UIViewUITableViewCellwithout中有 customAutoLayout并且您想在方向更改时更新 UI,您可以在UITableViewCell自定义类中执行此操作:

override func layoutSubviews() {
    super.layoutSubviews()
    self.customView.layoutSubviews()
}
于 2017-06-07T16:53:55.703 回答
0

你需要看的两件事是:

  1. 首先,检查设备是否旋转以重新加载您的表使用

    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ [self.tableView reloadData]; }

  2. 其次,在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中,使用 UIInterfaceOrientationIsLandscape(self.interfaceOrientation) 检查 GUI 状态以加载所需的布局。

于 2012-10-26T04:26:33.330 回答
0

其实我也做过同样的事情。首先,您需要为不同的方向制作两个 UITableViewCell.xib,然后在您的 viewController 中实现以下方法。

您不需要在 customCell.xib ViewController 中检测设备方向自动管理 tabelView 方向。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{

    if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone)
    {
        if(self.interfaceOrientation==UIInterfaceOrientationPortrait || self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
        {


        }
        else
        {


        }
    }
    else
    {

    }
[yourTable reloadData];
}    

然后在你的 tabelView CellForRowAtIndexPath 方法中实现下面的代码

if(self.interfaceOrientation==UIInterfaceOrientationPortrait || self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) 
        {  

            static NSString *CellIdentifier = @"Cell";
            CategoryCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[CategoryCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
                cell.autoresizingMask = UIViewAutoresizingFlexibleWidth;

            }
}
else{
            static NSString *CellIdentifier = @"Cell1";
            CategoryCell1 *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[CategoryCell1 alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
                cell.autoresizingMask = UIViewAutoresizingFlexibleWidth;
                            }
于 2012-10-23T08:52:16.707 回答