【问题标题】:iPhone sdk how to handle orientation in UITableView CellforRowAtIndexPath?iPhone sdk 如何处理 UITableView CellforRowAtIndexPath 中的方向?
【发布时间】:2013-01-31 06:43:22
【问题描述】:

我有 UIImageView、UILabel,它是在 UITableView CellforRowAtIndexPath 中创建的,因为我需要将每个单元格中的所有内容添加到我的 cell.contentview 中。但现在在处理横向方向方面面临问题。由于我在 CellforRowAtIndexPath 中定位了图像和标签,因此在为横向旋转时也获得了相同的位置(纵向)。我的代码,

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

    imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(680, 60, 40, 40)];
    [imgView1 setImage:[UIImage imageNamed:@"Downloaded.png"]];

      if([[[appDelegate selectDB] valueForKey:@"bookname"] containsObject:[listOfBooks objectAtIndex:indexPath.row]] )
      {
            [cell.contentView addSubview:imgView1];
      }
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{

      [self handleInterfaceRotationForOrientation:toInterfaceOrientation];

 if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) 
 {
      imgView1.frame = CGRectMake(890,0, 40, 40);
 }
 else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) 
 {
      imgView1.frame = CGRectMake(890,0, 40, 40);
 }
 else if (toInterfaceOrientation==UIInterfaceOrientationPortrait)
 {
      imgView1.frame = CGRectMake(690, 60, 40, 40);
 }
 else 
 {
      imgView1.frame = CGRectMake(690, 60, 40, 40);
 }
}

我也试过这个,

UIInterfaceOrientation orientation =  [[UIDevice currentDevice] orientation];

    if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150,20,125,163)];
        imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(680, 60, 40, 40)];//self.view.bounds.size.width-
        [imgView1 setImage:[UIImage imageNamed:@"Downloaded.png"]];
        Label = [[UILabel alloc] initWithFrame:CGRectMake(300, 60, 370, 28)];
        subtitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(300,88 , 200, 23)];
        subtitleLabel1=[[UILabel alloc]initWithFrame:CGRectMake(395,88 , 200, 23)];
    }
    else
    {
        imgView = [[UIImageView alloc] initWithFrame:CGRectMake(230,50,125,163)];
        imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(930, 60, 40, 40)];
        [imgView1 setImage:[UIImage imageNamed:@"Downloaded.png"]];
        Label = [[UILabel alloc] initWithFrame:CGRectMake(420, 60, 370, 28)];
        subtitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(420,88 , 200, 23)];
        subtitleLabel1=[[UILabel alloc]initWithFrame:CGRectMake(515,88 , 200, 23)];
    }

当我尝试使用上述方法时,即使是纵向,我也只能获得横向位置。这里做错了什么?有人能帮我吗?提前非常感谢。

【问题讨论】:

    标签: ipad uitableview uiinterfaceorientation


    【解决方案1】:

    如果我正确阅读了您的代码(看起来有点不合适,但我想我明白了),您正试图在设备旋转时更改图像位置。在我看来,您的第二组代码应该可以工作,但我不知道您将该部分代码放在项目中的什么位置。只有当你把它放在正确的地方时它才会起作用。我会这样做:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    
        UIInterfaceOrientation orientation =  [[UIDevice currentDevice] orientation];
    
        if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150,20,125,163)];
            imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(680, 60, 40, 40)];//self.view.bounds.size.width-
            [imgView1 setImage:[UIImage imageNamed:@"Downloaded.png"]];
            Label = [[UILabel alloc] initWithFrame:CGRectMake(300, 60, 370, 28)];
            subtitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(300,88 , 200, 23)];
            subtitleLabel1=[[UILabel alloc]initWithFrame:CGRectMake(395,88 , 200, 23)];
        }
        else
        {
            imgView = [[UIImageView alloc] initWithFrame:CGRectMake(230,50,125,163)];
            imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(930, 60, 40, 40)];
            [imgView1 setImage:[UIImage imageNamed:@"Downloaded.png"]];
            Label = [[UILabel alloc] initWithFrame:CGRectMake(420, 60, 370, 28)];
            subtitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(420,88 , 200, 23)];
            subtitleLabel1=[[UILabel alloc]initWithFrame:CGRectMake(515,88 , 200, 23)];
        }
    
          if([[[appDelegate selectDB] valueForKey:@"bookname"] containsObject:[listOfBooks objectAtIndex:indexPath.row]] )
          {
                [cell.contentView addSubview:imgView1];
          }
    }
    

    所以您正在处理cellForRowAtIndexPath: 中所有单元格内容的位置。现在您只需要使用此代码来告诉您的 tableview 在旋转时要做什么:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
    {
        //self.tableView is the table containing your data
        //You can animate this with the duration passed as well.
        [self.tableView reloadData];
    }
    

    willRotateToInterfaceOrientation: 调用在 IOS6 中有限制。确保在您旋转设备时实际调用它。

    【讨论】:

      猜你喜欢
      • 2012-05-15
      • 2011-04-03
      • 1970-01-01
      • 2011-04-16
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      相关资源
      最近更新 更多