2012-08-02 113 views
0

我在一個視圖中有2張桌子。表A列出了一羣用戶。表B列出了一個用戶對象。在表A中選擇一行時,表B將重新加載屬於該用戶的對象。iOS - UITableViewCell的圖像在選擇時會顯示在桌面上?

因此,當用戶選擇表A中的一行時,單元格背景中的圖像將變爲高亮顯示的圖像版本。

這裏是背景圖像的普通版:default image

這裏是背景圖像的高亮版本:enter image description here

正如你所看到的,突出顯示的版本對右側的小箭頭它。這個箭頭超出了表格單元格的寬度。選擇該行後,圖像會根據情況發生更改,但圖像的大小會縮小以適合整個圖像。

我想要發生的是圖像在表格之外,或在選定行的表格之上。

我認爲的一種可能的解決方案是將表格居中在選定的行上,然後覆蓋該圖像,但如果用戶試圖在表格中滾動,則圖像需要移動,這將是一個很大的痛苦。

所以我想知道的是可以將單元格的大小擴展到它所選的表格之外?

在此先感謝!

編輯:

下不工作,只是爲了防止有人去嘗試:

[cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width+20, cell.frame.size.height)];

回答

1

將視圖clipsToBounds屬性設置爲NO將允許視圖在其自己的框架之外繪製。

在你的UITableViewController子類:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do what you normally do here, if anything then add... 

    self.view.clipsToBounds = NO; 
} 

這樣做有一個副作用,在那裏你會看到在的tableview而不是將它們部分地滾動到視圖的底部或頂部創建的完整細胞。將表視圖放入另一個視圖中,將clipToBounds設置爲YES,將表視圖的邊緣與屏幕邊緣對齊,或者將視圖覆蓋底部和頂部(如通常的UIToolbar和UINavigationBar)。

要讓UITableViewCell的selectedBackgroundView延伸過表視圖的框架,請創建UITableViewCell的子類並覆蓋layoutSubviews方法。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 

     self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YQGyZ"]]; 
     self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CQXYh"]]; 

     self.textLabel.backgroundColor = [UIColor clearColor]; 
     self.detailTextLabel.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    CGRect frame = self.selectedBackgroundView.frame; 
    frame.size.width += 13; // where 13 is the size of the arrow overhang from the same images 
    self.selectedBackgroundView.frame = frame; 

    // You can also change the location of the default labels (set their background colors to clear to see the background under them) 
    self.textLabel.frame = CGRectMake(70, 0, 148, 30); 
    self.detailTextLabel.frame = CGRectMake(70, 30, 148, 30); 
} 

祝你好運。

0

我會建議修改未選中單元格的背景,使得圖像資源它與所選背景的寬度相同,但側面只有一個透明的矩形。

相關問題