IOS swift 表格的增加删除

 

// 继承与uiviewcontroller的类中写需要添加的数据并回传回去

class JumpViewController: UIViewController {

    var button:UIButton?

    var textfild:UITextField?

// 协议

    var paramsProtocolDelegate: ParamsProtocol?

 

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

       

        

        textfild = UITextField(frame: CGRect(x: 40, y: 150, width: self.view.frame.size.width - 80, height: 50))

        textfild?.placeholder = "输入添加的内容"

        

        button = UIButton(type: .custom)

        button?.frame = CGRect(x: 40, y: 250, width: self.view.frame.size.width - 80, height: 50)

        button? .setTitle("确定添加", for: .normal)

        button? .setTitleColor(UIColor.white, for: .normal)

        button?.backgroundColor = UIColor.blue

        button?.addTarget(self, action: #selector(confrom), for: .touchUpInside)

        self .view.addSubview(button!)

        self.view .addSubview(textfild!)

    

    }

    

    // 点击按钮方法

    @objc func confrom(){

        self .start()

        self.navigationController?.popViewController(animated: true)

        

        

    }

// 定义协议用于回传之后另一个页面调用

protocol ParamsProtocol {

        func returnparam(tmpstr:String)

}

// 数据回传  

    func start(){

        let str = textfild?.text

// 给回传方法赋值

        self.paramsProtocolDelegate?.returnparam(tmpstr: str!)

    }

 

}

// 第二部 viewcontroller中添加表格和导航栏按钮 点击添加按钮可以跳转到另一个页面,然后进行数据传值//

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,ParamsProtocol{

   //  遵循协议实现添加

    func returnparam(tmpstr: String) {

        addStr = tmpstr

        print(addStr)

        self.TabArr?.addObjects(from: [addStr])

        self.tableview?.reloadData()

    }

    

   

   

    

    var addStr = String()

    

    var tableview:UITableView?

    var TabArr:NSMutableArray?

    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return (TabArr?.count)!

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)

        cell.textLabel?.text = TabArr?[indexPath.row] as? String

        return cell

    }

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

 

        // 添加导航栏按钮

        let AddbarButton = UIBarButtonItem(title: "添加", style: UIBarButtonItemStyle.plain, target: self, action: #selector(add))

        self.navigationItem.rightBarButtonItem = AddbarButton

        

//        let DeletebarButton = UIBarButtonItem(title: "删除", style: UIBarButtonItemStyle.plain, target: self, action: #selector(delet))

//        self.navigationItem.leftBarButtonItem = DeletebarButton

       

        

        TabArr = NSMutableArray.init(array: ["1","2","3","4","5","6","7","8",])

        tableview = UITableView.init(frame: self.view.frame, style: .plain)

        tableview?.delegate = self

        tableview?.dataSource = self

        self.view.addSubview(tableview!)

    }

    

    

    

    

    

    

    //  方法删除

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        TabArr?.remove(indexPath.row)

        if editingStyle == UITableViewCellEditingStyle.delete {

            self.TabArr?.remove(self.TabArr![indexPath.row])

        }

        self.tableview?.reloadData()

       

    }

    

    // 点击添加按钮

    @objc func add(){

 

        let jump = JumpViewController()

        jump.paramsProtocolDelegate = self

// 遵循代理

        self.navigationController?.pushViewController(jump, animated: true)

    }

   

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

 

    }

 

 

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值