Created
June 14, 2015 12:51
-
-
Save masakid/e07735870f80bb54fc62 to your computer and use it in GitHub Desktop.
Lesson16
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
// | |
// AddFormViewController.swift | |
// KadaiCheck | |
// | |
// | |
import UIKit | |
class AddFormViewController: UIViewController { | |
//モードの定義 | |
enum Mode { | |
case ADD, EDIT | |
} | |
var mode = Mode.ADD | |
var editText:String = "" | |
//追加の名前 | |
@IBOutlet weak var textFieldForName: UITextField! | |
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
switch mode { | |
case .ADD: | |
break | |
case .EDIT: | |
self.textFieldForName.text = editText | |
default: | |
break | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func saveNameForm(sender: AnyObject) { | |
//追加名を取得 | |
let addName:String = self.textFieldForName.text | |
switch mode { | |
case .ADD: | |
//共通データへ追加名を格納 | |
appDelegate.common.appendFruitsWithName(addName, isCheck: false) | |
//segueで遷移 | |
performSegueWithIdentifier("saveSegue", sender: sender) | |
case .EDIT: | |
//共通データへ追加名を格納 | |
appDelegate.common.updateFruitsWithName(addName, oldName: editText, isCheck: false) | |
//segueで遷移 | |
performSegueWithIdentifier("updateSegue", sender: sender) | |
default: | |
break | |
} | |
} | |
/* | |
// 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?) { | |
// Get the new view controller using segue.destinationViewController. | |
// Pass the selected object to the new view controller. | |
} | |
*/ | |
} |
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
// | |
// CommonData.swift | |
// KadaiCheck | |
// | |
// | |
import UIKit | |
class CommonData: NSObject { | |
let keyName:String = "name" | |
let keyCheck:String = "check" | |
//果物とチェックマークのDictionaryのリスト | |
var fruitsArr: Array<Dictionary<String, Any>> = [] | |
func appendFruitsWithName(name:String, isCheck:Bool){ | |
let keyFruitsCheck: Dictionary<String,Any> = [keyName:name, keyCheck:isCheck] | |
fruitsArr.append(keyFruitsCheck) | |
} | |
//共通データリストの更新 | |
func updateFruitsWithName(name:String, oldName:String, isCheck:Bool){ | |
var newfruitsArr: Array<Dictionary<String, Any>> = [] | |
let keyFruitsCheck: Dictionary<String,Any> = [keyName:name, keyCheck:isCheck] | |
for dicSet in fruitsArr { | |
if dicSet[keyName] as! String == oldName { | |
newfruitsArr.append(keyFruitsCheck) | |
} else { | |
newfruitsArr.append(dicSet) | |
} | |
} | |
fruitsArr = newfruitsArr | |
} | |
//番号を指定して取得 | |
//indexPath.rowを渡したら、対応する行が返却される | |
func displayRowNumberFruits(fruitsNum:Int) -> (name:String, isCheck:Bool){ | |
let fruit = self.fruitsArr[fruitsNum] | |
return ((fruit[keyName] as? String)!, (fruit[keyCheck] as? Bool)!) | |
} | |
} |
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 | |
// Kadai13 | |
// | |
// | |
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 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