2014-01-24 80 views
2

我正在开发一个应用程序,在iOS7。我有一个TableViewController,它的风格是分组的。 我成功实施了this解决方案,可以根据需要获取到TableView的曲线。iOS7奇怪的UITableViewCell选择

编辑: -我没有使用任何自定义类的单元格。我已经添加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; 
     } 
    } 
} 

这段代码的作用 - See this - before selection

但得到一个问题,当我选择一个单元格,它看起来怪异像this

我想该节以适当的方式。任何想法将不胜感激。

+1

我在这个问题只是偶然今天也。我刚刚将单元格的“selectionStyle”更改为“UITableViewCellSelectionStyleNone”,直到找到可靠的解决方案。单元格收到了选择,但没有突出显示。 – Isuru

回答

1

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

换句话说 - 减少表格视图的宽度以匹配当前行宽,并保持行的出价,因为它现在(它会像tableview一样宽)。您的选择现在也不会太宽。

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

希望有所帮助。

+0

请用清楚的答案来解答。如果你能详细说明答案,这将有所帮助 – iOSdev

+0

我不明白有什么需要详细说明的。在提供的图像中,您将表视图缩小为与行一样宽。然后选择将与行一样宽。设计并不要求tableview与整个屏幕一样宽。 – avuthless

0

UITableViewCells选择,所有子视图得到[UIColor clearColor]默认情况下backgroundcolor,细胞selectedBackgroundView是设置事情是如何看的一个。

在您的codesample中,您正在操作bakcgroundview以获得圆角,但只要选中一个单元格,此视图就会变为透明。我会建议覆盖setSelected,并在这里操作单元格子视图。

UITableViewCellsselectedBackgroundView文档:

默认值是在普通样式的表 (UITableViewStylePlain)和非无为部分基团表 UITableViewStyleGrouped)细胞为零。仅当选中单元格时,UITableViewCell才会将此 属性的值作为子视图添加。如果它不是零,或者在所有其他视图之后,它将 选定的背景视图作为子视图直接添加到背景 视图(backgroundView)之上。 调用setSelected:animated:使选定的背景视图到 使用alpha淡入淡出和淡出。

1

今天也有这个问题,并且这里的修复:

添加以下代码willDisplayCell的末尾:

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

CFRelease(pathRef); 
+0

这适用于所选单元格问题。 – nath