对于UITableViewCell的selectedBackgroundView 在group模式下的问题

      





网上的可行的一种解决方式:
//override setHighlighted to manually set the regular/highlighted background colors
- (
void )setSelected:( BOOL )selected animated:( BOOL )animated {
    [
super   setSelected :selected  animated :animated];
    
if (selected){
        
self . backgroundColor  = [ UIColor   greenColor ];
    }
    
else  {
        
self . backgroundColor  = [ UIColor   redColor ];
    }
}



第二种解决方式: http://sugartin.info/2011/06/22/uitableviewcellselection-style-custom-for-grouped-tables/
- ( UITableViewCell  *)tableView:( UITableView  *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath
{
    
static   NSString  *CellIdentifier =  @"Cell" ;
    
UITableViewCell  *cell = [tableView  dequeueReusableCellWithIdentifier :CellIdentifier];
    
if  (cell ==  nil ) {
        cell = [[
UITableViewCell   alloc initWithStyle : UITableViewCellStyleValue1   reuseIdentifier :CellIdentifier];
        cell.
accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
        
UIImageView  *v = [[ UIImageView   alloc init ];
        v.
backgroundColor  = [ UIColor   clearColor ];  // put clear color
        [v 
setClipsToBounds : YES ];
        cell.
selectedBackgroundView  = v;
        [cell 
setClipsToBounds : YES ];
    }
    
UIImageView  *imgVBG=( UIImageView *)[cell  selectedBackgroundView ];
    
if (indexPath. row == 0 ){
        imgVBG.
image =[ UIImage   imageNamed : @"TopImg.png" ];
    }
    
else   if ((indexPath. section == 0  && indexPath. row == 4 )||(indexPath. section == 1  && indexPath. row == 7 )){
            imgVBG.
image =[ UIImage   imageNamed : @"BottomImg.png" ];  }
    
else  {
        imgVBG.
image =[ UIImage   imageNamed : @"MiddleImg.png" ];
    }
    
return  cell;

}

Images are as follows.








第三种方式:在cell中重载   http://blog.sina.com.cn/s/blog_6ae8b50d01019jq0.html
// 关键
- ( void )tableView:( UITableView  *)tableView willDisplayCell:( UITableViewCell  *)cell forRowAtIndexPath:( NSIndexPath  *)indexPath {
    
// cell.selectedBackgroundView.frame is not sized correctly yet, so rounding the corners has to be deferred briefly
    [[
self   class deferredRoundCornersForSelectedBackgroundViewForTableView :tableView  cell :cell  indexPath :indexPath];
}

+(
void )roundCorners:( UIRectCorner )corners forView:( UIView  *)view {
    
UIBezierPath  *maskPath = [ UIBezierPath   bezierPathWithRoundedRect :view. bounds
                                                   
byRoundingCorners :corners
                                                         
cornerRadii : CGSizeMake ( 10.0 10.0 )];
    
CAShapeLayer  *maskLayer = [ CAShapeLayer   layer ];
    maskLayer.
frame  = view. bounds ;
    maskLayer.
path  = maskPath. CGPath ;
    view.
layer . mask  = maskLayer;
}
+(
void )roundCornersForSelectedBackgroundViewForTableView:( UITableView  *)tableView cell:( UITableViewCell  *)cell indexPath:( NSIndexPath  *)indexPath {
    
NSUInteger  rows = [tableView  numberOfRowsInSection :indexPath. section ];
    
if  (rows ==  1 ) {
        [[
self   class roundCorners : UIRectCornerAllCorners   forView :cell. selectedBackgroundView ];
    } 
else   if  (indexPath. row  ==  0 ) {
        [[
self   class roundCorners : UIRectCornerTopLeft  |  UIRectCornerTopRight   forView :cell. selectedBackgroundView ];
    } 
else   if  (indexPath. row  == rows- 1 ) {
        [[
self   class roundCorners : UIRectCornerBottomLeft  |  UIRectCornerBottomRight   forView :cell. selectedBackgroundView ];
    } 
else  {
        [[
self   class roundCorners : 0   forView :cell. selectedBackgroundView ];
    }
}
+(
void )deferredRoundCornersForSelectedBackgroundViewForTableView:( UITableView  *)tableView cell:( UITableViewCell  *)cell indexPath:( NSIndexPath  *)indexPath {
    
int64_t  delayInSeconds =  0 ;
    
dispatch_time_t  popTime =  dispatch_time ( DISPATCH_TIME_NOW , delayInSeconds *  NSEC_PER_SEC );
    
dispatch_after (popTime,  dispatch_get_main_queue (), ^( void ){
        [[
self   class roundCornersForSelectedBackgroundViewForTableView :tableView  cell :cell  indexPath :indexPath];
    });
}



第四种方式:
核心思想是自己来画的
代码如下

typedef   enum   {
    CustomCellBackgroundViewPositionTop, 
    CustomCellBackgroundViewPositionMiddle, 
    CustomCellBackgroundViewPositionBottom,
    CustomCellBackgroundViewPositionSingle,
    CustomCellBackgroundViewPositionPlain
} CustomCellBackgroundViewPosition;

@interface  CustomCellBackgroundView :  UIView  {
    
UIColor  *borderColor;
    
UIColor  *fillColor;
    
CustomCellBackgroundViewPosition  position;
}

@property ( nonatomic retain UIColor  *borderColor, *fillColor;
@property ( nonatomic CustomCellBackgroundViewPosition  position;
@end

#import  "CustomCellBackgroundView.h"

static   void  addRoundedRectToPath( CGContextRef  context,  CGRect  rect,
                                 
float  ovalWidth, float  ovalHeight);

@implementation  CustomCellBackgroundView
@synthesize  borderColor, fillColor, position;

#define ROUND_SIZE  10
- ( BOOL ) isOpaque {
    
return   NO ;
}

- (
id )initWithFrame:( CGRect )frame {
    
if  ( self  = [ super   initWithFrame :frame]) {
        
// Initialization code
    }
    
return   self ;
}

- (
void )drawRect:( CGRect )rect {
    
// Drawing code
    
CGContextRef  c =  UIGraphicsGetCurrentContext ();
    
CGContextSetFillColorWithColor (c, [ fillColor   CGColor ]);
    
CGContextSetStrokeColorWithColor (c, [ borderColor   CGColor ]);
    
    
if  ( position  ==  CustomCellBackgroundViewPositionTop ) {
        
CGContextFillRect (c,  CGRectMake ( 0.0f , rect. size . height  -  10.0f , rect. size . width 10.0f ));
        
CGContextBeginPath (c);
        
CGContextMoveToPoint (c,  0.0f , rect. size . height  -  10.0f );
        
CGContextAddLineToPoint (c,  0.0f , rect. size . height );
        
CGContextAddLineToPoint (c, rect. size . width , rect. size . height );
        
CGContextAddLineToPoint (c, rect. size . width , rect. size . height  -  10.0f );
        
CGContextStrokePath (c);
        
CGContextClipToRect (c,  CGRectMake ( 0.0f 0.0f , rect. size . width , rect. size . height  -  10.0f ));
    } 
else   if  ( position  ==  CustomCellBackgroundViewPositionBottom ) {
        
CGContextFillRect (c,  CGRectMake ( 0.0f 0.0f , rect. size . width 10.0f ));
        
CGContextBeginPath (c);
        
CGContextMoveToPoint (c,  0.0f 10.0f );
        
CGContextAddLineToPoint (c,  0.0f 0.0f );
        
CGContextStrokePath (c);
        
CGContextBeginPath (c);
        
CGContextMoveToPoint (c, rect. size . width 0.0f );
        
CGContextAddLineToPoint (c, rect. size . width 10.0f );
        
CGContextStrokePath (c);
        
CGContextClipToRect (c,  CGRectMake ( 0.0f 10.0f , rect. size . width , rect. size . height ));
    } 
else   if  ( position  ==  CustomCellBackgroundViewPositionMiddle ) {
        
CGContextFillRect (c, rect);
        
CGContextBeginPath (c);
        
CGContextMoveToPoint (c,  0.0f 0.0f );
        
CGContextAddLineToPoint (c,  0.0f , rect. size . height );
        
CGContextAddLineToPoint (c, rect. size . width , rect. size . height );
        
CGContextAddLineToPoint (c, rect. size . width 0.0f );
        
CGContextStrokePath (c);
        
return // no need to bother drawing rounded corners, so we return
    }
    
    
// At this point the clip rect is set to only draw the appropriate
    
// corners, so we fill and stroke a rounded rect taking the entire rect
    
    
CGContextBeginPath (c);
    
addRoundedRectToPath (c, rect,  10.0f 10.0f );
    
CGContextFillPath (c);  
    
    
CGContextSetLineWidth (c,  1 );  
    
CGContextBeginPath (c);
    
addRoundedRectToPath (c, rect,  10.0f 10.0f );  
    
CGContextStrokePath (c);     
}


- (
void )dealloc {
    [
borderColor   release ];
    [
fillColor   release ];
    [
super   dealloc ];
}


@end

static   void  addRoundedRectToPath( CGContextRef  context,  CGRect  rect,
                                 
float  ovalWidth, float  ovalHeight)

{
    
float  fw, fh;
    
    
if  (ovalWidth ==  0  || ovalHeight ==  0 ) { // 1
        
CGContextAddRect (context, rect);
        
return ;
    }
    
    
CGContextSaveGState (context); // 2
    
    
CGContextTranslateCTM  (context,  CGRectGetMinX (rect), // 3
                           
CGRectGetMinY (rect));
    
CGContextScaleCTM  (context, ovalWidth, ovalHeight); // 4
    fw = 
CGRectGetWidth  (rect) / ovalWidth; // 5
    fh = 
CGRectGetHeight  (rect) / ovalHeight; // 6
    
    
CGContextMoveToPoint (context, fw, fh/ 2 );  // 7
    
CGContextAddArcToPoint (context, fw, fh, fw/ 2 , fh,  1 ); // 8
    
CGContextAddArcToPoint (context,  0 , fh,  0 , fh/ 2 1 ); // 9
    
CGContextAddArcToPoint (context,  0 0 , fw/ 2 0 1 ); // 10
    
CGContextAddArcToPoint (context, fw,  0 , fw, fh/ 2 1 );  // 11
    
CGContextClosePath (context); // 12
    
    
CGContextRestoreGState (context); // 13
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值