iOS7中奇怪的UITableViewCell选择

我正在iOS7中开发一个应用程序。 我有一个TableViewController ,它的样式是分组的。 我成功实现了这个解决方案,可以根据需要获得TableView曲线。

编辑: –我没有使用任何自定义类的单元格。 我刚从nib添加了TableView ,并且默认使用了单元格。

实施的守则如下: –

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(tintColor)]) { if (tableView == self.tblviwSample) { CGFloat cornerRadius = 5.f; cell.backgroundColor = UIColor.clearColor; CAShapeLayer *layer = [[CAShapeLayer alloc] init]; CGMutablePathRef pathRef = CGPathCreateMutable(); CGRect bounds = CGRectInset(cell.bounds, 10, 0); BOOL addLine = NO; if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius); } else if (indexPath.row == 0) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)); addLine = YES; } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds)); } else { CGPathAddRect(pathRef, nil, bounds); addLine = YES; } layer.path = pathRef; CFRelease(pathRef); layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor; if (addLine == YES) { CALayer *lineLayer = [[CALayer alloc] init]; CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale); lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight); lineLayer.backgroundColor = tableView.separatorColor.CGColor; [layer addSublayer:lineLayer]; } UIView *testView = [[UIView alloc] initWithFrame:bounds]; [testView.layer insertSublayer:layer atIndex:0]; testView.backgroundColor = UIColor.clearColor; cell.backgroundView = testView; } } } 

此代码的效果 – 请在选择之前查看此内容

但是当遇到一个问题时,当我选择一个单元格时,它看起来很奇怪

我希望这个部分得到适当的方式。 任何想法将不胜感激。

我会使tableview更薄(从两侧减小宽度),然后实现你的外观,以便该行不会在表视图的边缘之前结束,而是一直延伸到tableview的一侧。

换句话说 – 减少表视图宽度以匹配当前行宽并将该行保留为现在的出价(然后它将与tableview一样宽)。 您的选择现在也不会太宽。

或者,在选择后,您可以修改表格视图单元格,以便模仿某些自定义选择。 这可能是通过改变单元格颜色直到用动画来翻转它。

希望有所帮助。

今天也有这个问题,这是修复:

将以下代码添加到willDisplayCell的末尾:

 CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = cell.bounds; maskLayer.path = pathRef; cell.selectedBackgroundView.layer.mask = maskLayer; CFRelease(pathRef); 

选择UITableViewCells ,默认情况下所有子视图都会获得[UIColor clearColor]backgroundcolor [UIColor clearColor] ,并且selectedBackgroundView是设置事物外观的单元格。

在您的codeample中,您正在操纵bakcgroundview以获得圆角,但是一旦选择了单元格,该视图就变得透明。 我建议覆盖setSelected ,并在这里操纵单元子视图。

来自UITableViewCells selectedBackgroundView文档:

对于普通样式表(UITableViewStylePlain)中的单元格,默认值为nil;对于节组表UITableViewStyleGrouped,默认值为nil。 仅当选择单元格时,UITableViewCell才会将此属性的值添加为子视图。 它将选定的背景视图添加为背景视图(backgroundView)正上方的子视图(如果它不是nil),或者在所有其他视图后面。 调用setSelected:animated:使选定的背景视图以alpha淡入淡出为动画显示。