Swift带图删除

2016-11-09  本文已影响62人  LennonLin
// 直接调用
func setRootVc() {
        let vc = ViewController()  // 创建一个类
        let nav = UINavigationController(rootViewController: vc) // 方法在括号里
        self.window?.rootViewController = nav
        
    }

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    let LXKDeleteTableViewCellIdentifier = "LXKDeleteTableViewCellIdentifier"
   // let cell: LXKDeleteTableViewCell!
    
    //MARK: Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        self.title = "删除cell"
        self.addSubView()
    }
    
    // MARK: Private
   // 私有只有当前源文件可以使用 默认Internal 可以访问自己模块或者应用中源文件的任何实体 public:  可以访问自己模块或者应用中源文件的任何实体 并且别人也可以访问 感觉不怎么懂.....
private   func addSubView() {
        tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        self.view .addSubview(tableView)
        
        tableView.register(LXKDeleteTableViewCell.self, forCellReuseIdentifier: LXKDeleteTableViewCellIdentifier)
    }
    
    // MARK: TableViewDatasource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: LXKDeleteTableViewCellIdentifier)
        cell?.textLabel?.text = "第\(indexPath.row)行"
        return cell!
    }
    
    // MARK: TableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80;
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        // 只是刷新界面没有移除数据
        self.tableView .reloadData()
    }
    
    // 一个简单的yes已经如此复杂了
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
       return (UITableViewCellEditingStyle.init(rawValue: 1) != nil)
    }
}

import UIKit

class LXKDeleteTableViewCell: UITableViewCell {
    var deleteView: UIView!
    // 参考文章: http://draveness.me/swift-zhong-init-de-shi-yong/
    // 必须要一个以上的指定构造器 Designated Initializer
    // 指定构造器是类的主要构造器, 要在指定构造器中初始化所有的属性, 并且要在调用父类合适的指定构造器
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 便利构造器
    override init (style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.setDeleteView()
    }
    
    
    func setDeleteView() {
        
        // 宽度不能只为80 不然后面会露出系统的删除
        deleteView = UIView.init(frame: CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: 80 ))
        deleteView.backgroundColor = UIColor.red
        self.contentView .addSubview(deleteView)
        
        let deleteButton = UIButton()
        deleteButton.backgroundColor = UIColor.red
        deleteButton.frame = CGRect(x: 0 , y: 0, width: 80, height: 80)
        deleteButton.setImage(UIImage(named:"del"), for:UIControlState.normal)
        deleteButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        let leftMargin: CGFloat = 27
        deleteButton.imageEdgeInsets = UIEdgeInsetsMake(10, leftMargin, 20, leftMargin)
        deleteButton.titleEdgeInsets = UIEdgeInsetsMake(40, 0, 0, leftMargin - 3)
        deleteButton.setTitle("删除", for:UIControlState.normal)
        deleteButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        deleteView .addSubview(deleteButton)
    }
}

参考:swift访问控制

参考:swift构造器

上一篇下一篇

猜你喜欢

热点阅读