2016-05-15 90 views
0

請在下面找到代碼,我需要在我的tableviewcontroller視圖頂部的編輯按鈕,它可以重新排列或刪除行。任何幫助將不勝感激。我發現了一個內置函數來刪除單個行,但我特別需要一個編輯按鈕。Tableview控制器如何添加編輯按鈕重新排列行IOS Swift 2

import UIKit 

    class TableViewController: UITableViewController { 
     var sports:[Sport] = [] 
     override func viewDidLoad() { 
      loadData() 
      super.viewDidLoad() 

      // Uncomment the following line to preserve selection between presentations 
      // self.clearsSelectionOnViewWillAppear = false 

      // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
      // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
     } 
     func loadData(){ 




      if let path = NSBundle.mainBundle().pathForResource("data", ofType: "plist"){ 
       if let tempDict = NSDictionary(contentsOfFile: path){ 
        let tempArray = (tempDict.valueForKey("sports") as! NSArray) as Array 
        for dict in tempArray { 

         let sportName = dict["sportName"]! as! String 

         let numberOfPlayers = dict["numberOfPlayers"]! as! String 

         let countryOfOrigin = dict["countryOfOrigin"]! as! String 


         let p = Sport(sportName: sportName, numberOfPlayers:numberOfPlayers, countryOfOrigin:countryOfOrigin) 
         sports.append(p) 
        } 
       } 

      } 
     } 
     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 

     // MARK: - Table view data source 

     override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      // #warning Incomplete implementation, return the number of sections 
      return 1 
     } 

     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      // #warning Incomplete implementation, return the number of rows 
      return sports.count 
     } 


     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) 
      var sport = sports[indexPath.row] 

      cell.textLabel?.text = sport.getSportName() 
      cell.detailTextLabel?.text = sport.getCountryOfOrigin() 

      // Configure the cell... 

      return cell 
     } 


     /* 
     // Override to support conditional editing of the table view. 
     override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
      // Return false if you do not want the specified item to be editable. 
      return true 
     } 
     */ 

     /* 
     // Override to support editing the table view. 
     override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
      if editingStyle == .Delete { 
       // Delete the row from the data source 
       tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
      } else if editingStyle == .Insert { 
       // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
      }  
     } 
     */ 

     /* 
     // Override to support rearranging the table view. 
     override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 

     } 
     */ 

     /* 
     // Override to support conditional rearranging of the table view. 
     override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
      // Return false if you do not want the item to be re-orderable. 
      return true 
     } 
     */ 

     /* 
     // 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. 
     } 
     */ 
     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      let sport = sports[indexPath.row] 
      let detailVC = SportsDetailVC(style: .Grouped) 
    //  detailVC.title = park.title 
      detailVC.sport = sport 
    //  detailVC.zoomDelegate = mapVC 
      navigationController?.pushViewController(detailVC, animated: true) 

     } 

    } 


import UIKit 

class SportsDetailVC: UITableViewController { 
    var sport:Sport! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return 1 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") 
     if cell == nil{ 
      cell = UITableViewCell(style: .Default, reuseIdentifier:"resuseIdentifier") 
     } 
     cell?.textLabel?.text = sport.getNumberOfPlayers() 
     // Configure the cell... 

     return cell! 
    } 


    /* 
    // Override to support conditional editing of the table view. 
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     // Return false if you do not want the specified item to be editable. 
     return true 
    } 
    */ 

    /* 
    // Override to support editing the table view. 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == .Delete { 
      // Delete the row from the data source 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     } else if editingStyle == .Insert { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
     }  
    } 
    */ 

    /* 
    // Override to support rearranging the table view. 
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 

    } 
    */ 

    /* 
    // Override to support conditional rearranging of the table view. 
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     // Return false if you do not want the item to be re-orderable. 
     return true 
    } 
    */ 

    /* 
    // 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. 
    } 
    */ 

} 
+0

使用'editButtonItem()'功能基。 – rmaddy

回答

0

生成的代碼中包含了以下意見:

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
// self.navigationItem.rightBarButtonItem = self.editButtonItem() 

取消對下聯:

self.navigationItem.rightBarButtonItem = self.editButtonItem() 

您可能還需要實現一些其他的代表功能的功能工作:

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) 
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool 

例如,除非您實施適當的委託方法,否則您點擊/按住以重新排序項目的右側小條將不可用。

+0

非常感謝!這工作完美:) –

+0

優秀!知道這解決了你的問題可能對他人有所幫助(接受的答案) – user212514

相關問題