Advertisement
thieumao

UITableView Demo with Add, Edit

May 10th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.94 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  4.  
  5.     @IBOutlet weak var myTable: UITableView!
  6.    
  7.     @IBOutlet weak var lblMonHoc: UILabel!
  8.     @IBOutlet weak var tfInput: UITextField!
  9.  
  10.     @IBAction func Add(sender: AnyObject) {
  11.         mangMonHoc.append(tfInput.text!)
  12.         myTable.reloadData()
  13.     }
  14.    
  15.     @IBAction func EditButton(sender: AnyObject) {
  16.         myTable.editing = !myTable.editing
  17.     }
  18.    
  19.     var mangMonHoc:[String] = ["iOS", "Android" ]
  20.    
  21.     override func viewDidLoad() {
  22.         super.viewDidLoad()
  23.         myTable.dataSource = self
  24.         myTable.delegate = self
  25.     }
  26.  
  27.     override func didReceiveMemoryWarning() {
  28.         super.didReceiveMemoryWarning()
  29.         // Dispose of any resources that can be recreated.
  30.     }
  31.    
  32.     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  33.         return 1
  34.     }
  35.  
  36.     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  37.         return mangMonHoc.count
  38.     }
  39.    
  40.     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  41.         let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
  42.         cell.textLabel?.text = mangMonHoc[indexPath.row]
  43.         return cell
  44.     }
  45.    
  46.     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  47.         print("Click " + String(indexPath.row))
  48.         lblMonHoc.text = mangMonHoc[indexPath.row]
  49.     }
  50.    
  51.     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  52.         if editingStyle == UITableViewCellEditingStyle.Delete {
  53.             mangMonHoc.removeAtIndex(indexPath.row)
  54.             myTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement