2015-10-20 40 views
1

在我的應用程序中我有一個tableView女巫我有默認文本,我想有一個滑動編輯文本功能,以便用戶可以更改文本,如果想要。 我已經添加了滑動刪除,使用下面的代碼,但添加一個編輯文本函數並不容易找到信息,所以我的問題是,我如何添加一個按鈕,以便用戶可以通過編輯文本刷卡功能? 我需要知道的代碼加載文本,因爲我不能有一個文本框,所以它不是的東西一樣容易像label.text = textfield.text 該原始負荷文本如下如何滑動來編輯tableView中的文本?

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

     cell.textLabel?.text = places[indexPath.row]["name"] 

感謝代碼!

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

     if editingStyle == UITableViewCellEditingStyle.Delete { 

      places.removeAtIndex(indexPath.row) 

      NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places") 

      tableView.reloadData() 

     } 
      } 

回答

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

    var editAction = UITableViewRowAction(style: .Normal, title: "Edit") { (action, indexPath) in 
       tableView.editing = false 

    // your action 
      } 

      editAction.backgroundColor = UIColor.grayColor() 


    var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) in 
       tableView.editing = false 
    // your delete action 
    } 

    return [deleteAction, editAction] 
+0

謝謝,麻煩的是給編輯細胞的作用。我原來有默認文本編碼如下,如果這有幫助:重寫func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell let cell = tableView.dequeueReusableCellWithIdentifier(「Cell」,forIndexPath:indexPath) cell.textLabel ?.text = places [indexPath.row] [「name」] –

相關問題