iPhone sdk如何在UITableView CellforRowAtIndexPath中处理方向?

时间:2013-01-31 06:43:23

标签: ipad uitableview uiinterfaceorientation

我有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)];
    }

当我尝试使用上述方法时,即使是在肖像中我也只能获得风景位置。这里有什么问题?有人能帮我吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

如果我正确地阅读你的代码(它似乎有点不合适,但我认为我得到了图片)你试图在设备旋转时改变图像位置。在我看来,你的第二组代码应该可行,但我不知道你在那个代码段放在项目中的哪个位置。它只会在你把它放在正确的地方才有效。我会做这样的事情:

- (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中有限制。确保在旋转设备时实际调用它。