看到没,在gropp模式下,边界越界了,这个的解决方式有
http://stackoverflow.com/questions/10219560/clear-answer-on-how-to-mask-a-uiview-as-a-uitableviewcell-selectedbackgroundview/10219699#10219699
网上的可行的一种解决方式:
//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
{- ( 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.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
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
}