美文网首页
[iOS]多选中设置UITableViewCell默认选择为灰色

[iOS]多选中设置UITableViewCell默认选择为灰色

作者: PlutoMa | 来源:发表于2016-04-28 20:58 被阅读1151次

UITableView中多选是非常常见的,我们一般使用系统自带的多选按钮,这个按钮是蓝色的。最近项目里有个需求,某些按钮是默认选中的,并且是灰色的,苦于没有UI设计,只能自己来改变系统的选择按钮了,选择按钮颜色的改变可以参考以前的一篇文章给图片进行颜色渲染
首先,我们把图片渲染的方法封装起来,方便调用。

import UIKit

class MDMTintImageHelper {
    static func tintedImageWithColor(color: UIColor, originalityImage: UIImage!) -> UIImage {
        //创建图片位置大小
        let imageRect = CGRect(x: 0.0, y: 0.0, width: originalityImage.size.width, height: originalityImage.size.height)
        
        //设置绘制图片上下文
        UIGraphicsBeginImageContextWithOptions(imageRect.size, false, originalityImage.scale)
        //得到上下文
        let context = UIGraphicsGetCurrentContext()
        //绘制图片
        originalityImage.drawInRect(imageRect)
        //设置渲染颜色
        CGContextSetFillColorWithColor(context, color.CGColor)
        //设置透明度(值可根据需求更改)
        CGContextSetAlpha(context, 0.5)
        //设置混合模式
        CGContextSetBlendMode(context, CGBlendMode.SourceAtop)
        //设置位置大小
        CGContextFillRect(context, imageRect)
        
        //绘制图片
        let imageRef = CGBitmapContextCreateImage(context)
        let darkImage = UIImage(CGImage: imageRef!, scale: originalityImage.scale, orientation: originalityImage.imageOrientation)
        
        //完成绘制
        UIGraphicsEndImageContext()
        return darkImage
    }
}

然后,我们自定义一个cell,用来表示已经默认选择的cell。

import UIKit

class MDMSelectedCell: UITableViewCell {
    override func layoutSubviews() {
        super.layoutSubviews()
        
        for subview in self.subviews {
            if subview.isKindOfClass(NSClassFromString("UITableViewCellEditControl")!) {
                let imageView = subview.valueForKey("_imageView") as? UIImageView
                if imageView != nil {
                    imageView!.image = MDMTintImageHelper.tintedImageWithColor(UIColor.lightGrayColor(), originalityImage: imageView!.image)
                }
            }
        }
        
    }
}

最后,我们对tableview进行展示,这里默认偶数行是默认选择的。

import UIKit

class MDMViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var tableView: UITableView?
    lazy var dataArray: [String] = {
        var array = [String]()
        for i in 0 ..< 100 {
            array.append(String(i))
        }
        return array
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView = UITableView(frame: self.view.bounds, style: .Plain)
        view.addSubview(tableView!)
        tableView?.dataSource = self
        tableView?.delegate = self
        tableView?.registerClass(MDMSelectedCell.classForCoder(), forCellReuseIdentifier: String(MDMSelectedCell.classForCoder()))
        tableView?.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: String(UITableViewCell.classForCoder()))
        tableView?.editing = true
    }
    

}

extension MDMViewController {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row % 2 == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier(String(MDMSelectedCell.classForCoder()), forIndexPath: indexPath)
            cell.textLabel?.text = dataArray[indexPath.row]
            tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier(String(UITableViewCell.classForCoder()), forIndexPath: indexPath)
            cell.textLabel?.text = dataArray[indexPath.row]
            return cell
        }
        
        
    }
    
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle(rawValue: (1 | 2))!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row % 2 == 0 {
            tableView .selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
        } else {
            //TODO 可选中cell选中需进行的操作
        }
    }
    
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row % 2 == 0 {
            tableView .selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
        } else {
            //TODO 可选中cell取消选中需进行的操作
        }
    }
    
}

需要注意的是,如果操作类型设置为多选,那么,

tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)

这个方法是被屏蔽的。
附属完整代码连接:
Demo地址

相关文章

网友评论

      本文标题:[iOS]多选中设置UITableViewCell默认选择为灰色

      本文链接:https://www.haomeiwen.com/subject/sjjhrttx.html