swift-tableView 代理方法实现点击按钮删除效果

swift-tableView 代理方法实现点击按钮删除效果,第1张

概述import UIKitprotocol FirstTableViewCellDelegate: NSObjectProtocol { func deleteCell(cell: FirstTableViewCell, button: UIButton)}class FirstTableViewCell: UITableViewCell { @IBOutlet
import UIKitprotocol FirsttableVIEwCellDelegate: NSObjectProtocol {        func deleteCell(cell: FirsttableVIEwCell,button: UIbutton)}class FirsttableVIEwCell: UItableVIEwCell {    @IBOutlet weak var myLabel: UILabel!    @IBOutlet weak var mybutton: UIbutton!    weak var delegate:FirsttableVIEwCellDelegate?        overrIDe func awakeFromNib() {        super.awakeFromNib()        prepareUI()        // Initialization code    }    overrIDe func setSelected(selected: Bool,animated: Bool) {        super.setSelected(selected,animated: animated)        // Configure the vIEw for the selected state    }        func prepareUI() {                mybutton.setTitle("删除",forState: UIControlState.normal)        mybutton.backgroundcolor = UIcolor.redcolor()        mybutton.layer.cornerRadius = 15        mybutton.layer.masksToBounds = true        mybutton.setTitlecolor(UIcolor.whitecolor(),forState: UIControlState.normal)        mybutton.addTarget(self,action: "BtnClick:",forControlEvents: UIControlEvents.touchUpInsIDe)    }        @objc private func BtnClick(button:UIbutton){                delegate?.deleteCell(self,button: button)    }    }

import UIKitclass FirstVIEwController: UIVIEwController {    @IBOutlet weak var tableVIEw: UItableVIEw!        let cellIDentifIEd = "cell"    var numbers = ["One","Two","Three","Foure","Five","Six","Seven","Eight","Nine","Ten"];        overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // 准备UI        prepareUI()        // Do any additional setup after loading the vIEw.    }   private func prepareUI() {            navigationItem.Title = "首页"            tableVIEw.delegate = self        tableVIEw.dataSource = self    //        tableVIEw.registerClass(UItableVIEwCell.self,forCellReuseIDentifIEr: cellIDentifIEd)        tableVIEw.registerNib(UINib(nibname: "FirsttableVIEwCell",bundle: nil),forCellReuseIDentifIEr: cellIDentifIEd)                }    overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }        func tableVIEw(tableVIEw: UItableVIEw,commitEditingStyle editingStyle: UItableVIEwCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {        if editingStyle == UItableVIEwCellEditingStyle.Delete {            numbers.removeAtIndex(indexPath.row)            tableVIEw.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UItableVIEwRowAnimation.automatic)        }    }    /*    // MARK: - Navigation    // In a storyboard-based application,you will often want to do a little preparation before navigation    overrIDe func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {        // Get the new vIEw controller using segue.destinationVIEwController.        // Pass the selected object to the new vIEw controller.    }    */    }extension FirstVIEwController:UItableVIEwDelegate,UItableVIEwDataSource,FirsttableVIEwCellDelegate {        func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell {        //        let cell = tableVIEw.dequeueReusableCellWithIDentifIEr(cellIDentifIEd,forIndexPath: indexPath)         let cell = tableVIEw.dequeueReusableCellWithIDentifIEr(cellIDentifIEd,forIndexPath: indexPath) as! FirsttableVIEwCell        //        cell.textLabel?.text = numbers[indexPath.row]        cell.myLabel.text = numbers[indexPath.row]        cell.delegate = self        cell.selectionStyle = UItableVIEwCellSelectionStyle.None        return cell    }        func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {        return numbers.count    }            func deleteCell(cell: FirsttableVIEwCell,button: UIbutton) {                let IDx:NSIndexPath = tableVIEw.indexPathForCell(cell)!                numbers.removeAtIndex(IDx.row)        tableVIEw.deleteRowsAtIndexPaths([IDx],withRowAnimation: UItableVIEwRowAnimation.top)    }}
总结

以上是内存溢出为你收集整理的swift-tableView 代理方法实现点击按钮删除效果全部内容,希望文章能够帮你解决swift-tableView 代理方法实现点击按钮删除效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081588.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存