2015-09-29 31 views
1
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 


    @IBOutlet weak var tableView: UITableView! 

    var items = ["foo", "bar", "baz", "mamma mia"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tableView.editing = true 
     navigationItem.leftBarButtonItem = editButtonItem() 
    } 

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

    func tableView(table1: UITableView, 
     numberOfRowsInSection section: Int) 
     -> Int { 
      return self.items.count 
    } 

    // `UITableViewCell`s for each section and row 
    func tableView(tableView: UITableView, 
     cellForRowAtIndexPath indexPath: NSIndexPath) 
     -> UITableViewCell { 
      let item = self.items[indexPath.row] 

      let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as UITableViewCell 
      cell.textLabel!.text = item 
      return cell 
    } 

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     print ("at editing style") 
     if editingStyle == .Delete { 

      print ("delete at row: \(indexPath.row)") 
     } 
     if editingStyle == .Insert { 
      print ("insert at row: \(indexPath.row)") 

     } 
    } 

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
//  print ("at can edit row: \(indexPath.row)") 

     return true 
    } 


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 

    } 
} 
  1. 爲什麼菜單/刪除按鈕沒有出現?
  2. 如果(何時)出現,它們是否在commitEditingStyle處理?

when pressing the button no delete/menu option appears. why ?tableview編輯模式不允許刪除和/或菜單:缺少什麼?

回答

0

實現此方法:

func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle { 
    return .Delete 
} 
+0

做到這一點,但仍然沒有改變。當按下「 - 」按鈕時,行內容變爲空白,並且不會出現刪除/其他選項。使用Xcode 7.0(7A121I) –

+0

OOps。發現問題。 –

+0

對不起:問題是AUTOLAYOUT。一旦我解決了約束條件,信息就會按照需要顯示。感謝您的幫助.... –

0
 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 

     let shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action, indexPath) -> Void in 
//   tableView.editing = false 
      print("shareAction") 
     } 
     let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (action, indexPath) -> Void in 
//   tableView.editing = false 
      print("deleteAction") 
     } 
//  shareAction.backgroundColor = UIColor.grayColor() 
     return [deleteAction, shareAction] 
    } 
相關問題