2015-04-02 56 views
1

我想在用戶點擊編輯按鈕時用UITableView左邊的加入按鈕在左側插入新單元格,並在用戶單擊完成按鈕時消失。現在我在表格視圖的最後一行獲得加號按鈕,但我無法動態添加新行。任何人都可以幫助我應該做什麼或者他們有更好的方式來做到這一點?在UITableViewin swift中點擊編輯按鈕的新行

我去了這個答案在堆棧溢出Using insert rows in a UITableView ,但我沒有得到

NSMutableArray* paths = [[NSMutableArray alloc] init]; 

這是什麼說法是。

這是我的代碼

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if self.tableView.editing == true { 
      return nameArray.count + 1 
     } 
     else 
     { 
      return nameArray.count 
     } 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

     if indexPath.row >= nameArray.count 
     { 
      cell.textLabel?.text = "insert new row" 
     } 
     else 
     { 
      cell.textLabel?.text = nameArray[indexPath.row] 
     } 

     cell.showsReorderControl = true 
     return cell 
} 


override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == .Delete { 
      nameArray.removeAtIndex(indexPath.row) 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     } 
     else if editingStyle == .Insert{ 
      self.performSegueWithIdentifier("insertNameSegue", sender:nil) 
     } 
} 


override func setEditing(editing: Bool, animated: Bool) { 
     super.setEditing(true, animated: true) 
     self.tableView.setEditing(true, animated: true) 
     tableView.reloadData() 

} 
+0

您需要實現'didSelectRowAtIndexPath' - 在這方法檢查你是否正在編輯,如果它是最後選擇的「添加」行 - 如果是,請執行你的插入視圖到插入視圖 – Paulw11 2015-04-02 07:12:41

回答

0

這是客觀的C:

NSMutableArray* paths = [[NSMutableArray alloc] init]; 

對於斯威夫特,你應該使用數組

var paths = Array<NSIndexPath>() 
paths.append(NSIndexPath(10, 0)) 
...