在分组表视图中更改选定的单元格

时间:2016-01-15 10:04:06

标签: ios swift tableview

我创建了一个带有分组样式的静态单元格表​​视图 enter image description here

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

这是我的代码,可以实现该部分的圆角。

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

3 个答案:

答案 0 :(得分:0)

如果您只想更改单元格的选定颜色,可以执行以下操作:

目标-C:

$(function() {
    $(document.body).on("click",".clickable",function(e) {
        e.preventDefault();
        show(this.getAttribute("data-for"));
    });

    function show(sel) { ... }
});

夫特:

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

对于细胞角落:

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

答案 1 :(得分:0)

根据您的实现,selectedBackgroundView还有一个UITableViewCell属性,因此您只需在方法的末尾添加此代码。

我刚用另一个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

答案 2 :(得分:0)

Hello lopez你要做的就是在UITableViewCell上选择diable选择样式

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

2 Swift 另一种方法可能是

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

}

快乐编程