Add swipe to delete UITableViewCell

后端 未结 25 1582
暖寄归人
暖寄归人 2020-11-30 17:50

I am making a CheckList application with a UITableView. I was wondering how to add a swipe to delete a UITableViewCell.

This is my ViewCont

相关标签:
25条回答
  • 2020-11-30 18:34
        import UIKit
    
        class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource
        {
          var items: String[] = ["We", "Heart", "Swift","omnamay shivay","om namay bhagwate vasudeva nama"]
            var cell : UITableViewCell
    }
    
    
    
    
        @IBOutlet var tableview:UITableView
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    
    
        func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
            return self.items.count;
        }
    
    
        func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    
            var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
    
            if !cell {
                cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")}
    
    
    
            cell!.textLabel.text = self.items[indexPath.row]
    
            return cell
            }
        func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
            return true
        }
    
        func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
            if (editingStyle == UITableViewCellEditingStyle.Delete) {
                // handle delete (by removing the data from your array and updating the tableview)
    
    
                if let tv=tableView
                {
    
    
    
                 items.removeAtIndex(indexPath!.row)
                    tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    
    
    
            }
        }
    }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题