1

我正在嘗試設置一個可以在右上角顯示圖像的UITableViewCell。爲什麼在旋轉到橫向時,此UIImage從UITableViewCell消失?

我讓它爲縱向模式工作,但是當我旋轉到橫向時,圖像消失。

下面的代碼:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     UIImage *cornerImage = [UIImage imageNamed:@"star_corner.png"]; 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width - cornerImage.size.width, 
                       0, 
                       cornerImage.size.width, 
                       cornerImage.size.height)]; 
     imageView.tag = kCornerImageViewTag; 
     [cell.contentView addSubview:imageView]; 
     [imageView release]; 
    } 

    UIImageView *theView = (UIImageView *)[cell.contentView viewWithTag:kCornerImageViewTag]; 
    [theView setImage: [UIImage imageNamed:@"star_corner.png"]]; 

    cell.textLabel.text = @"the text label"; 
    return cell; 
} 

有趣的是,如果我註釋掉「cell.textLabel.text =」行,圖像不顯示在景觀...雖然它沒有轉移過來的最右邊。

如果還有什麼我在這裏做錯了,請讓我知道。

在此先感謝您的幫助。

回答

2

我想通了。解決方案是創建兩個UIImageView的......一個用於星形圖像,另一個用於單元格背景圖像。

我將UIImageView作爲子視圖添加到背景圖像UIImageView中,然後將其分配給cell.backgroundView。

星的UIImageView職位本身正確地使用:

starImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 

而且由於整個事情被分配到cell.backgroundView(而不是cell.contentView),它不受細胞的刪除按鈕或重新排序控件。

0

你的UIViewController應該響應didRotateFromInterfaceOrientation。

在此方法中,您可以遍歷單元格,使用[單元格viewWithTag]獲取UIImageView並根據新的單元維度(cell.contentView.bounds)修改框架屬性。

祝你好運!

+0

感謝您的回覆。我實際上是響應didRotateFromInterfaceOrientation。 自從幾個月前發佈這個問題後,我發現簡單的解決方案只是添加: imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 將圖像正確放置在橫向上。但是,由於調出單元格的「刪除」按鈕會導致圖像移動,所以它仍然不適用於我。 從做進一步的研究,似乎我需要子類UITableViewCell並做我的佈局 - 所以我正在探索如何做到這一點。 – 2010-08-18 17:12:16