2011-08-02 154 views
4

任何想法如何在分組的uitableview的邊框周圍顯示陰影?(圍繞頂部,底部,邊上的所有邊界以及部分的圓角周圍)。分組的uitableview與陰影

我需要完全一樣在這裏的圖片一樣的效果:

alt http://img824.imageshack.us/img824/2826/tableviewwithshadow.png

注重邊境附近的小影(灰色的,藍色背景)

+0

http://cocoawithlove.com/2009/08/adding-shadow-effects-to-uitableview.html – ram

+0

嘗試這些鏈接。它可以幫助你找到你正在尋找的東西。 http://stackoverflow.com/questions/3436276/uilabel-shadow-from-custom-cell-selected-color http://stackoverflow.com/questions/4925282/showing-shadow-in-a-cell-inside-table -view http://stackoverflow.com/questions/3546880/iphone-uitabelviewcell-layer-shadow – iOS

回答

11

我想出了一個相當「黑客」的解決方案,在我看來,所以我並不完全滿意,但無論如何!由此實現了在一節中圍繞一組單元繪製陰影的基本功能。因此,我的子類的UITableView並實施了layoutSubviews方法是這樣的:

- (void) layoutSubviews { 
    [super layoutSubviews]; 

    const CGFloat PageCellBackgroundRadius = 6.0; 

    for(int i = 0; i < [self numberOfSections]; i++) { 
     NSInteger viewTag = i + 123456; 
     CGRect frameRect = [self shadowFrameForSection: i]; 

     UIView* shadowBackgroundView = [self viewWithTag: viewTag]; 
     if (shadowBackgroundView) { 
      if (!CGRectEqualToRect(frameRect, shadowBackgroundView.frame)) { 
       shadowBackgroundView.frame = frameRect; 
       CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds 
                  byRoundingCorners: UIRectCornerAllCorners 
                    cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath; 
       shadowBackgroundView.layer.shadowPath = shadowPath; 
      } 

      [self sendSubviewToBack: shadowBackgroundView]; 
     } else { 
      shadowBackgroundView = [[[UIView alloc] initWithFrame: frameRect] autorelease]; 
      shadowBackgroundView.tag = viewTag; 
      shadowBackgroundView.opaque = YES; 
      shadowBackgroundView.backgroundColor = [UIColor clearColor]; 

      shadowBackgroundView.layer.shadowOpacity = 0.3; 
      shadowBackgroundView.layer.shadowRadius = 2; 
      shadowBackgroundView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
      shadowBackgroundView.layer.shadowOffset = CGSizeMake(0.0, 1.0); 
      CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds 
                 byRoundingCorners: UIRectCornerAllCorners 
                   cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath; 
      shadowBackgroundView.layer.shadowPath = shadowPath; 
      shadowBackgroundView.layer.shouldRasterize = YES; 

      [self addSubview: shadowBackgroundView]; 
     } 
    } 
} 

和一個小的輔助方法:

- (CGRect) shadowFrameForSection: (NSInteger) section { 
    CGRect sectionFrame = [self rectForSection: section]; 

    CGFloat sectionHeaderHeight = CGRectGetHeight([self rectForHeaderInSection: section]); 
    CGFloat sectionFooterHeight = CGRectGetHeight([self rectForFooterInSection: section]); 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(sectionHeaderHeight + 1, 10, sectionFooterHeight + 1, 10); 
    return UIEdgeInsetsInsetRect(sectionFrame, contentInsets); 
} 

代碼是不言自明,我認爲! 基本上它圍繞添加UIViews作爲子視圖的UITableView,計算一個節的框架,然後將該陰影繪製到添加的UIView的圖層上。 UITableView提供了 一些框架計算方法:「rectForSection」等

我不滿意它,因爲它感覺不對,在layoutSubviews方法中添加視圖! (這可以做其他=「表視圖委託?) 我也會喜歡更多的工作與CALayers,但你不能標記他們!因爲我的代碼通過 縮小/擴展已經添加uiviews性能的原因。

以這種形式

,該解決方案將不會如部分消失或工作,如果段reorderd等 也表現不好,如果你在一個動畫的方式添加行表視圖等 但它是相當對於「靜態」分組表格視圖非常有用!

我也沒有測試過性能影響,所以請自行承擔風險!

所以也許這是一個很好的起點,可能更好的解決方案! :-)

+0

這個想法很明確,但代碼並不像預期的那樣工作,至少在iOS 5上。它實際上在部分之上增加了一些黑霧,而不是在下面。即使我將[self sendSubviewToBack:shadowBackgroundView]也添加到「其他」,它也不起作用。似乎創建的視圖放在背景後面。如上所述, – HiveHicks

+0

,我的解決方案還有改進的空間! ;) –

+0

但無論如何:您是否使用默認窗體中的分組表格視圖樣式,或者您是否有例如自定義部分頁眉/頁腳視圖?如果是這樣,也許這就是該部分背景陰影視圖的「位移」的原因!在「shadowFrameForSection」中播放節頭/頁腳插入值,通過考慮自定義視圖的框架(如果有的話),爲背景視圖創建適當的框架/邊界! –

2

一個最簡單的是將圖層的陰影樣式添加到表格中的單元格。記住你會有三種造型 - 一種用於細胞的頂部,一種用於底部細胞,另一種用於其他細胞。如果你開始寫代碼應該不是很困難。

+0

我已經添加了一個鏈接到我想要實現的圖像。 –

+0

有趣的是,這裏有很多提議的解決方案...和兩年半後,我認爲這仍然是最好的方法。 – taylorcressy

0

cellForRowAtIndexPath:功能:

// drop shadow 
cell.layer.shadowOpacity = 1.0; 
cell.layer.shadowRadius = 1.7; 
cell.layer.shadowColor = [UIColor blackColor].CGColor; 
cell.layer.shadowOffset = CGSizeMake(0.0, 0.0);