現在メモアプリのようなものを作っているのですが、TableView上で新たなセルを追加したり削除したりという機能やそこから新たな画面遷移する機能は実装できたのですが、iOS標準のメモアプリのように新たなセル?を追加したらその都度そのセルに対応する新たな画面が作成されるようなプログラムができません。
調べながら試行錯誤してみてはいるのですが未だに実装できていないので質問させていただきます。
ちなみに現在のコードです(セルに対応する新たな画面作成に関するコードは試行錯誤中なので省いています)
lang
1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet weak var myTableView: UITableView! 6 7 var newTableView: UITableView! 8 9 let myItems: NSMutableArray = ["TEST1", "TEST2", "TEST3"] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 // Do any additional setup after loading the view, typically from a nib. 14 15 // 編集ボタンを追加. 16 self.navigationItem.rightBarButtonItem = self.editButtonItem() 17 18 // 編集中のセル選択を許可. 19 myTableView.allowsSelectionDuringEditing = true 20 21 //TableViewの背景色を変更 22 //self.myTableView.backgroundColor = UIColor.whiteColor() 23 24 } 25 26 override func didReceiveMemoryWarning() { 27 super.didReceiveMemoryWarning() 28 // Dispose of any resources that can be recreated. 29 } 30 31 /* 32 Cellの総数を返す 33 (実装必須) 34 */ 35 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 36 return myItems.count 37 } 38 39 /* 40 Cellに値を設定する 41 (実装必須) 42 */ 43 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 44 45 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 46 47 // Cellに値を設定. 48 cell.textLabel?.text = "\(myItems[indexPath.row])" 49 50 return cell 51 } 52 53 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 54 // Return false if you do not want the specified item to be editable. 55 return true 56 } 57 58 59 /* 60 編集ボタンが押された際に呼び出される 61 */ 62 override func setEditing(editing: Bool, animated: Bool) { 63 super.setEditing(editing, animated: animated) 64 65 // TableViewを編集可能にする 66 myTableView.setEditing(editing, animated: true) 67 68 // 編集中のときのみaddButtonをナビゲーションバーの左に表示する 69 if editing { 70 println("編集中") 71 let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addCell:") 72 self.navigationItem.setLeftBarButtonItem(addButton, animated: true) 73 } else { 74 println("通常モード") 75 self.navigationItem.setLeftBarButtonItem(nil, animated: true) 76 } 77 } 78 79 /* 80 addButtonが押された際呼び出される 81 */ 82 func addCell(sender: AnyObject) { 83 println("追加") 84 85 // myItemsに追加. 86 myItems.addObject("new") 87 88 // TableViewを再読み込み. 89 myTableView.reloadData() 90 } 91 92 /* 93 Cellを挿入または削除しようとした際に呼び出される 94 */ 95 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 96 97 // 削除のとき. 98 if editingStyle == UITableViewCellEditingStyle.Delete { 99 println("削除") 100 101 // 指定されたセルのオブジェクトをmyItemsから削除する. 102 myItems.removeObjectAtIndex(indexPath.row) 103 104 // TableViewを再読み込み. 105 myTableView.reloadData() 106 } 107 } 108 109 /* 110 Cellが選択された際に呼び出される. 111 112 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 113 114 // 選択中のセルが何番目か. 115 println("Num: \(indexPath.row)") 116 117 // 選択中のセルのvalue. 118 println("Value: \(myItems[indexPath.row])") 119 120 // 選択中のセルを編集できるか. 121 println("Edeintg: \(tableView.editing)") 122 123 124}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/05/20 03:57
2015/05/20 07:16
2015/05/20 07:24
2015/05/20 14:07
2015/05/20 15:56
2015/05/21 03:16
2015/05/21 03:27
2015/05/21 04:13
2015/05/21 04:27
2015/05/21 11:52
2015/05/21 11:55
2015/05/21 14:44
2015/05/21 14:45
2015/05/21 14:55
2015/05/21 15:02
2015/05/21 15:16