swift UITableViewCell插入单无格

<span style="font-family:Arial, Helvetica, sans-serif;">自学菜鸟,因为看了一文章,说是写博客可帮助成长。所以这只是记录,没有什么技术含量。如果有写错的地方,还望指正。</span>
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var months = ["january", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    var tableView = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "tableView"
        
        tableView = UITableView(frame: view.bounds, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        
        //默认状态下,开启表格的编辑模式
        tableView.setEditing(true, animated: false)
        //注册cell
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "month")
        self.view.addSubview(tableView)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return months.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let ID = "month"
        var cell = tableView.dequeueReusableCell(withIdentifier: ID)
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: ID)
        }
        cell!.textLabel!.text = months[indexPath.row]
        return cell!
    }
    
    //添加一个代理方法,用来设置单元格的编辑模式为插入模式
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.insert
    }
    
    //响应单元格的插入事件
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        //判断如果编辑模式为插入,则执行之后的代码
        if editingStyle == .insert {
            //获取插入位置的单元格,在段落中的行数
            let rowNum = indexPath.row
            //往数组中同步插入新数据,及时更新数据源
            months.insert("moon", at: rowNum)
            
            //创建一个包含待插入单元格位置信息的新数组
            let indexPaths = [indexPath]
            //往表格视图中的指定位置,插入新的单元格
            tableView.insertRows(at: indexPaths, with: UITableViewRowAnimation.right)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}




tableView.setEditing(true, animated: false) 代码决定了 TabelViewcell 前面的状态“+”。若想去前面除“+”号,

可先设置左侧编辑按扭为系统自带的专门实现编辑功能的按扭:

self.navigationItem.leftBarButtonItem = =self.editButtonItem
重写setEditing方法:

  //当点击系统处自带的编辑按扭时,调用此方法
    //参数 editing 为是否编辑
    //参数 animated 为是否显示动画
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        
        tableView.setEditing(editing, animated: animated)
        tableView.setEditing(true, animated: true)
    }


                                          




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值