Created
June 20, 2015 00:10
-
-
Save masakid/16b1baf08e07c15b4c31 to your computer and use it in GitHub Desktop.
Lesson17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // TableViewController.swift | |
| // | |
| import UIKit | |
| class TableViewController: UITableViewController { | |
| //共通で保持するデータ | |
| var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate | |
| //データ受け渡し用 | |
| var temp:String = "" | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Uncomment the following line to preserve selection between presentations | |
| // self.clearsSelectionOnViewWillAppear = false | |
| // Uncomment the following line to display an Edit button in the navigation bar for this view controller. | |
| // self.navigationItem.rightBarButtonItem = self.editButtonItem() | |
| var fruitsArr = appDelegate.common.fruitsArr | |
| //初回実行時のみデータを格納 | |
| if fruitsArr.count == 0 { | |
| appDelegate.common.appendFruitsWithName("りんご", isCheck: false) | |
| appDelegate.common.appendFruitsWithName("みかん", isCheck: false) | |
| appDelegate.common.appendFruitsWithName("バナナ", isCheck: true) | |
| appDelegate.common.appendFruitsWithName("パイナップル", isCheck: false) | |
| } | |
| } | |
| @IBAction func exitAddFormSave(segue:UIStoryboardSegue){ | |
| } | |
| @IBAction func exitEditFormSave(segue:UIStoryboardSegue){ | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| // MARK: - Table view data source | |
| override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
| // #warning Potentially incomplete method implementation. | |
| // Return the number of sections. | |
| return 1 | |
| } | |
| override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| // #warning Incomplete method implementation. | |
| // Return the number of rows in the section. | |
| //共通データの配列で行を作る | |
| return appDelegate.common.fruitsArr.count | |
| } | |
| override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
| let cell = tableView.dequeueReusableCellWithIdentifier("FCell", forIndexPath: indexPath) as! TableViewCell | |
| // Configure the cell... | |
| //共通データから取得 | |
| let item = appDelegate.common.displayRowNumberFruits(indexPath.row) | |
| cell.headImage.image = nil | |
| //画像データの設定 | |
| if item.isCheck { | |
| cell.headImage.image = UIImage(named:"check") | |
| } | |
| //テキストデータの設定 | |
| cell.label1.text = (item.name) ?? "" | |
| return cell | |
| } | |
| @IBAction func exitAddFormCancel(segue:UIStoryboardSegue){ | |
| } | |
| //追加画面から戻ってきたときにテーブルを再描画する | |
| override func viewWillAppear(animated: Bool) { | |
| self.tableView.reloadData() | |
| super.viewWillAppear(animated) | |
| } | |
| //行を押した時のアクション | |
| override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
| let cell = tableView.dequeueReusableCellWithIdentifier("FCell", forIndexPath: indexPath) as! TableViewCell | |
| //共通データから取得 | |
| var item = appDelegate.common.displayRowNumberFruits(indexPath.row) | |
| //データ更新 | |
| appDelegate.common.updateFruitsWithName(item.name, oldName: item.name, isCheck: !item.isCheck) | |
| tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade) | |
| } | |
| override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { | |
| let cell = tableView.dequeueReusableCellWithIdentifier("FCell", forIndexPath: indexPath) as! TableViewCell | |
| //共通データから取得 | |
| var item = appDelegate.common.displayRowNumberFruits(indexPath.row) | |
| //データ保持 | |
| temp = item.name | |
| performSegueWithIdentifier("editSegue", sender: nil) | |
| } | |
| //削除する際のメソッド | |
| override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { | |
| if editingStyle == .Delete { | |
| appDelegate.common.fruitsArr.removeAtIndex(indexPath.row) | |
| tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) | |
| } | |
| } | |
| /* | |
| // Override to support conditional editing of the table view. | |
| override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { | |
| // Return NO 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 | |
| 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) { | |
| } | |
| */ | |
| /* | |
| // Override to support conditional rearranging of the table view. | |
| override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { | |
| // Return NO if you do not want the item to be re-orderable. | |
| return true | |
| } | |
| */ | |
| // MARK: - Navigation | |
| // In a storyboard-based application, you will often want to do a little preparation before navigation | |
| override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
| var nav = segue.destinationViewController as? UINavigationController | |
| var view = nav?.topViewController as? AddFormViewController | |
| switch segue.identifier ?? "" { | |
| case "addSegue": | |
| view?.mode = .ADD | |
| case "editSegue": | |
| view?.mode = .EDIT | |
| view?.editText = temp | |
| default: | |
| break | |
| } | |
| // Get the new view controller using [segue destinationViewController]. | |
| // Pass the selected object to the new view controller. | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment