【问题标题】:Change Selected cell in Grouped Table View更改分组表视图中的选定单元格
【发布时间】:2026-01-10 11:35:01
【问题描述】:

我创建了一个具有分组样式的静态单元格表​​视图

从上图中可以看出,我的问题是当我单击任何不对应于白色背景的单元格时会看到附件和不透明。有人可以帮我解决这个问题吗?

这是我实现部分圆角的代码。

 override func viewDidLoad() {
        self.tableView.separatorColor = UIColor.clearColor()
    }



    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        if (cell.respondsToSelector(Selector("tintColor"))){

            if (tableView == self.tableView) {

                let cornerRadius : CGFloat = 12.0
                cell.backgroundColor = UIColor.clearColor()
                let layer: CAShapeLayer = CAShapeLayer()
                let pathRef:CGMutablePathRef = CGPathCreateMutable()
                let bounds: CGRect = CGRectInset(cell.bounds, 25, 0)
                var addLine: Bool = false

                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 = true

                } 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 = true

                }

                layer.path = pathRef
                layer.fillColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.8).CGColor

                if (addLine == true) {
                    let lineLayer: CALayer = CALayer()
                    let lineHeight: CGFloat = (1.0 / 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)
                }

                let testView: UIView = UIView(frame: bounds)
                testView.layer.insertSublayer(layer, atIndex: 0)
                testView.backgroundColor = UIColor.clearColor()
                cell.backgroundView = testView
            }
        }

【问题讨论】:

  • 为什么你要使用这么多逻辑来设计你的单元格而不是使用故事板定制?
  • 实际上该代码仅适用于每组圆角。注意不是表格单元格。

标签: ios swift tableview


【解决方案1】:

如果你只想改变单元格的选定颜色,你可以这样做:

目标-C:

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor whiteColor];
[cell setSelectedBackgroundView:bgColorView];

斯威夫特:

var bgColorView = UIView()
bgColorView.backgroundColor = UIColor.whiteColor()
cell.selectedBackgroundView = bgColorView

对于单元格角轮:

cell.contentView.layer.cornerRadius = 5
cell.contentView.layer.masksToBounds = true 

【讨论】:

  • 其实我想要的就像 ipad 常规设置一样。这就是我发布的图片中包含披露指示符的原因。
【解决方案2】:

你好洛佩兹,你所要做的就是禁用 UITableViewCell 上的选择样式

1) iOS cell.selectionStyle = UITableViewCellSelectionStyleNone; or [cell setAlpha:0.5];

2 斯威夫特 另一种方法可能是

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
carTableView?.deselectRowAtIndexPath(indexPath, animated: true)

}

快乐编程

【讨论】:

  • 抱歉,我不想禁用它。
【解决方案3】:

随着您的实现,UITableViewCell 还有一个selectedBackgroundView 属性,因此您可以在方法末尾添加此代码。

我刚刚使用您创建的路径副本创建了另一个带有另一个 shapeLayer 的视图,更改了 fillColor 以指示选择。

        let selectedLayer = CAShapeLayer()
        selectedLayer.path = CGPathCreateCopy(pathRef)
        selectedLayer.fillColor = UIColor(white: 0.75, alpha: 0.8).CGColor
        let selectedView = UIView(frame: bounds)
        selectedView.layer.insertSublayer(selectedLayer, atIndex: 0)
        selectedView.backgroundColor = UIColor.clearColor()
        cell.selectedBackgroundView = selectedView

【讨论】:

  • 非常感谢。它有效,但我该如何修复披露指标?如果您能帮助我了解该指标,我真的会将您的答案标记为正确。
  • 哦,可能没有非骇人听闻的方法来更改默认披露指示器的框架。您可以创建自己的附件视图,看起来像一个披露指示器,并将其设置到单元格中,以便您可以为其提供自定义框架。
最近更新 更多