2016-04-09 20 views
1

私はいくつかの記事を読んだことがありますが、自分のコードで実装する方法は見当たりません。私はオブジェクトライブラリからドラッグした棒ボタンアイテム(編集)を持っています。仕事はしていますが、編集モードで「完了」と表示したいと思います。どんな助けでも大歓迎です。私のコードは次のようになります。編集中に[編集]ボタンを[完了]にするにはどうすればよいですか?

@IBAction func editButton(sender: AnyObject) { 
    self.editing = !self.editing { 

// 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 
     lessons.removeAtIndex(indexPath.row) 
     saveLessons() 
     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) { 

    let itemToMove = lessons[fromIndexPath.row] 

    lessons.removeAtIndex(fromIndexPath.row) 

    lessons.insert(itemToMove, atIndex: toIndexPath.row) 

    saveLessons() 

} 

答えて

2

オーバーライドsetEditing

override func setEditing(editing: Bool, animated: Bool) { 
    super.setEditing(editing, animated:animated) 
    if editing == true { 
    // set title of the bar button to 'Done' 
    editBarButton.title = "Done"  
    } else { 
    // set title of the bar button to 'Edit' 
    editBarButton.title = "Edit" 
    } 
} 

か短いの三項条件演算子

override func setEditing(editing: Bool, animated: Bool) { 
    super.setEditing(editing, animated:animated) 
    editBarButton.title = editing ? "Done" : "Edit" 
} 
+0

おかげで、ヴァディアンを使用して - あなたはタイトルを設定するのですか? – Laroms

+1

ボタンへの参照が必要です。あなたのデザインによって異なります。 Interface Builderのあらかじめ定義された 'rightBarButtonItem'または' leftBarButtonItem'をボタンに割り当てたり、カスタムIBOutletを作成したりできます。 – vadian

+0

カスタムIBOutlet(IBOutlet weak var editBarButton)を作成しましたが、メソッドに書き込む方法はわかりません。私は最初のタイトルを「完了」、2番目のタイトルを「編集」にしたいと思います。 – Laroms

関連する問題