2012-04-04 152 views
2

我想設置2個完全不同的外觀(和內容)的單元格。 (一個選中,一個未選中)。UITableView單元格:selectedBackgroundView而不顯示backgroundView

此代碼有效,但是,因爲選定單元格的圖像是50%透明的,我可以在底部看到BackgroundView。

很明顯,selectedBackgroundView提供了一種很好的方式來填充單元格,並且元素僅在選中時可見。難道還有比BackgroundView來填充單元的另一個位置,位置怎麼也不會出現在selectedBackgroundView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"hello"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 


    UIImage *image = [UIImage imageNamed:@"listview_btn.png"]; 
    UIImage *imageThumb = [UIImage imageNamed:@"01_thumb.jpg"]; 
    UIImageView* imageView = [[UIImageView alloc] initWithImage:imageThumb]; 
    [imageView setFrame:CGRectMake(0, 0, 50, 67)]; 
    [cell setBackgroundView:[[UIImageView alloc]initWithImage:image] ]; 
    [cell.backgroundView addSubview:imageView]; 



    UIImage *imageSel = [UIImage imageNamed:@"listview_btn_on.png"]; 
    UIImage *imageThumbSel = [UIImage imageNamed:@"01_thumb_Sel.jpg"]; 
    UIImageView* imageViewSel = [[UIImageView alloc] initWithImage:imageThumbSel]; 
    [imageViewSel setFrame:CGRectMake(110, 0, 50, 67)]; 
    [cell setSelectedBackgroundView:[[UIImageView alloc]initWithImage:imageSel] ]; 
    [cell.selectedBackgroundView addSubview:imageViewSel]; 
+0

我花了一次點擊來修復你的代碼格式。也許你應該在下次點擊。爲此,請選擇您插入到問題中的所有代碼,然後按下「{}」按鈕。它會添加適當的降價縮減,所以你的代碼是可讀的,並獲得很好的配色方案:-) – 2012-04-04 02:55:21

+1

當然,它是[Xcode](http://stackoverflow.com/a/5725282/457406)而不是xCode。 Xcode不是iPhone。 Xcode只是一個IDE,它是編寫代碼的工具。如果你的問題不是關於IDE本身的(例如「如何在Xcode中打開行號?」,請不要使用標籤 – 2012-04-04 02:59:09

+0

這個問題沒有答案?(感謝Matthias的更正) – Franck 2012-04-07 12:57:32

回答

0

請選擇背景圖不透明。由於它總是插在背景視圖上方,因此可以將它們組合到Photoshop或其他圖形編輯器中,並將結果用作選定的背景視圖。

如果您不能負擔這種做法(我還是極力推薦它),你也可以繼承UITableViewCell,並同時覆蓋setHighlighted:animatedsetSelected:animated選擇/高亮時背景視圖屬性設置爲無,然後再取消/未加亮時恢復它。但是,這是一個更爲複雜的方法,恕我直言。

相關問題