1

我有一个 tableView,我必须根据设备是纵向还是横向来定义不同的单元格高度,但似乎这种方法无法识别设备方向。此方法不起作用:

  -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


  UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]orientation];//here doesn't recognize the orientation


 if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
     if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
         self.tableView.rowHeight =33.33333;

     }
     else if (deviceOrientation== UIInterfaceOrientationLandscapeRight || deviceOrientation==UIInterfaceOrientationLandscapeLeft){

         self.tableView.rowHeight = 16.333333;
     }
 }

 else {
     if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown){
         self.tableView.rowHeight = 66.66666;

     }else if (deviceOrientation== UIInterfaceOrientationLandscapeRight || deviceOrientation==UIInterfaceOrientationLandscapeLeft){
         self.tableView.rowHeight = 33.333333;

     }

 }
 return self.tableView.rowHeight;

}
4

2 回答 2

0

采用:

UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;

识别 statusBar 方向,而不是:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]orientation];
于 2012-10-30T09:44:10.527 回答
0

使用单例类方法 UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation];

于 2012-10-30T10:26:57.570 回答