0

UITableView CellforRowAtIndexPathで作成したUIImageView、UILabelがあります。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)];
    }

上記の方法を使用しようとすると、縦向きでも横向きの位置しか得られません。ここで何が間違っていますか?誰かが私を助けることができますか?よろしくお願いします。

4

1 に答える 1

0

私があなたのコードを正しく読んでいる場合 (少し場違いに思えますが、画像が得られると思います)、デバイスの回転時に画像の場所を変更しようとしています。私の意見では、2 番目のコード セットは機能するはずですが、そのコード セクションをプロジェクトのどこに配置したのかわかりません。適切な場所にある場合にのみ機能します。私はこのようなことをします:

- (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:ます。これで、テーブルビューに回転時に何をすべきかを伝えるために、このコードが必要になるだけです:

- (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];
}

IOS6willRotateToInterfaceOrientation:では呼び出しに制限があります。デバイスを回転させたときに実際に呼び出されていることを確認してください。

于 2013-01-31T14:51:37.327 に答える