Skip to content

Instantly share code, notes, and snippets.

@masakid
Created June 13, 2015 11:14
Show Gist options
  • Save masakid/0a259251962862052909 to your computer and use it in GitHub Desktop.
Save masakid/0a259251962862052909 to your computer and use it in GitHub Desktop.
Lesson15
//
// CommonData.swift
// KadaiCheck
//
//
import UIKit
class CommonData: NSObject {
let keyName:String = "name"
let keyCheck:String = "check"
//果物とチェックマークのDictionaryのリスト
var fruitsArr: Array<Dictionary<String, Any>> = []
func setFruitsWithName(name:String, isCheck:Bool){
let keyFruitsCheck: Dictionary<String,Any> = [keyName:name, keyCheck:isCheck]
fruitsArr.append(keyFruitsCheck)
}
//共通データリストの更新
func updateFruitsWithName(name: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 == name {
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)!)
}
}
//
// TableViewCell.swift
// Kadai13
//
//
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var headImage: UIImageView!
@IBOutlet weak var label1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
//
// TableViewController.swift
// Kadai13
//
//
import UIKit
class TableViewController: UITableViewController {
//共通で保持するデータ
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
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.setFruitsWithName("りんご", isCheck: false)
appDelegate.common.setFruitsWithName("みかん", isCheck: false)
appDelegate.common.setFruitsWithName("バナナ", isCheck: true)
appDelegate.common.setFruitsWithName("パイナップル", isCheck: false)
}
}
@IBAction func exitAddFormSave(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)
//trueならfalse, falseならtrue
item.isCheck = item.isCheck ? false : true
appDelegate.common.updateFruitsWithName(item.name, isCheck: item.isCheck)
let indexPaths = [indexPath]
tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Fade)
}
/*
// 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?) {
// 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