简体   繁体   English

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

[英]Change Selected cell in Grouped Table View

I have create a Static Cells Table view with Grouped style 我创建了具有分组样式的静态单元格表​​视图 在此处输入图片说明

As you can see from the image above my problem is the accessory and opaque that will be seen when I click to any of the cell which does not corresponds to the white background.Can someone help me fix this problem? 从上图可以看到,我的问题是附件和不透明的东西,当我单击与白色背景不对应的任何单元格时都会看到它们。有人可以帮助我解决此问题吗?

Here's my code that achieves the rounded corners of the section. 这是我的代码,实现了该部分的圆角。

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

If you only want to change the selected color of the cell, you can do this: 如果只想更改单元格的选定颜色,则可以执行以下操作:

Objective-C: 目标C:

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

Swift: 迅速:

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

For cell corner rounds: 对于单元角圆:

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

Going with your implementation, there is also a selectedBackgroundView property for UITableViewCell , so you could just add this code at the end of the method. 在实现过程中,还为UITableViewCellselectedBackgroundView属性,因此您只需在方法末尾添加此代码即可。

I just created another view with another shapeLayer with the copy of the path you've created, changing the fillColor to indicate selection. 我刚刚使用创建的路径的副本创建了另一个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

Hello lopez all you have to do is to diable selection style on the UITableViewCell 您好lopez,您要做的就是在UITableViewCell上启用选择样式

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

2 Swift another approach may be 2 Swift的另一种方法可能是

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

} }

Happy Programming 快乐编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM