Swift带图删除

  • swift实现带图片的删除,如下图:


    屏幕快照 2016-11-09 下午6.32.58.png
  • delegate的函数:

// 直接调用
func setRootVc() {
        let vc = ViewController()  // 创建一个类
        let nav = UINavigationController(rootViewController: vc) // 方法在括号里
        self.window?.rootViewController = nav
        
    }
  • VC的代码,真心写的好不习惯,还体会不到他们说的优雅:

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)
    }
}
  • 定制的cell:

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构造器

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • ======================================================= i...
    nimomeng阅读 11,984评论 35 98
  • 永远都不会在来来回回折腾了, 比起身体的累更倾向于心累。 一生都渴望安稳。 又回到这里,太熟悉的陌生。 仿佛一切都...
    如初同学阅读 436评论 0 0
  • “姐姐,你真漂亮,我能请你喝杯果汁吗?” 稚嫩的童音打断我的沉思,我偏过头,看到声音的来源,一个八九岁的完美小正太...
    老猫solo阅读 683评论 24 13
  • 大家好,今天我正式加入了简书这个大家庭,非常开心,本人今年15岁,也拿过几个国家级的文学赛奖碑,以后会经常更文,欢...
    Sandm木木夏阅读 223评论 0 0