【问题标题】:Weird UITableViewCell selection in iOS7iOS7中奇怪的UITableViewCell选择
【发布时间】:2014-02-15 05:14:53
【问题描述】:

我正在 iOS7 中开发应用程序。我有一个TableViewController,它的风格是分组的。 我成功实现了this 解决方案,可以随心所欲地获得TableView 的曲线。

编辑:- 我没有为单元格使用任何自定义类。我刚刚从笔尖添加了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

我希望该部分以适当的方式。任何想法将不胜感激。

【问题讨论】:

  • 我今天也偶然发现了这个问题。我刚刚将单元格的selectionStyle 更改为UITableViewCellSelectionStyleNone,直到找到可靠的解决方案。单元格收到了选择,但没有突出显示它。

标签: ios uitableview ios7


【解决方案1】:

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

换句话说 - 减小 table view 的宽度以匹配当前的 line width 并让 line 保持现在的bid(然后它将与 tableview 一样宽)。现在,您的选择范围也会变小。

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

【讨论】:

  • 请回答清楚。如果您能详细说明答案会有所帮助
  • 我不明白有什么要详细说明的。您将表格视图缩小到与提供的图像中的线一样宽。然后选择也将与线条一样宽。设计不需要 tableview 和整个屏幕一样宽。
【解决方案2】:

When UITableViewCells are selected, all subviews get a backgroundcolor of [UIColor clearColor] by default, and the cells selectedBackgroundView is the one that sets how things are looking.

在您的代码示例中,您正在操纵 bakcgroundview 以获得圆角,但是一旦选择了一个单元格,该视图就会变得透明。我建议覆盖setSelected,并在此处操作单元格子视图。

来自UITableViewCellsselectedBackgroundView 文档:

对于普通样式表中的单元格,默认值为 nil (UITableViewStylePlain) 和非零用于部分组表 UITableViewStyleGrouped)。 UITableViewCell 添加了 this 的值 仅在选择单元格时将属性作为子视图。它增加了 选择背景视图作为背景正上方的子视图 view (backgroundView) 如果它不为零,或者在所有其他视图之后。 调用 setSelected:animated: 使选定的背景视图 使用 alpha 淡入淡出动画。

【讨论】:

    【解决方案3】:

    今天也遇到了这个问题,解决方法如下:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多