Last active
January 31, 2017 13:02
-
-
Save donguri9/51747dc5b580923b731957c251158249 to your computer and use it in GitHub Desktop.
ToDo TableView
This file contains 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
import UIKit | |
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{ | |
@IBOutlet weak var tableView: UITableView! | |
var todoList = [String]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let userDefaults = UserDefaults.standard | |
if let storedtodoList = userDefaults.array(forKey: "todoList") as? [String] { | |
todoList.append(contentsOf: storedtodoList) | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ | |
return todoList.count | |
} | |
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ | |
// セルを取得する | |
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) | |
// セルに表示する値を設定する | |
let todoTitle = todoList[indexPath.row] | |
// セルのラベルにToDoをセット | |
cell.textLabel!.text = todoTitle | |
return cell | |
} | |
/// セルが選択された時に呼ばれるデリゲートメソッド | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
print("セル番号:\(indexPath.row) セルの内容:\(todoList[indexPath.row])") | |
} | |
@IBAction func addButton(_ sender: Any) { | |
// アラートダイアログ生成 | |
let alertController = UIAlertController(title: "ToDoを追加", | |
message: "ToDoを入力してください", | |
preferredStyle: UIAlertControllerStyle.alert) | |
// テキストエリアを追加 | |
alertController.addTextField(configurationHandler: nil) | |
// OKボタンを追加 | |
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { | |
(action: UIAlertAction) -> Void in | |
// OKボタンが押されたときの処理 | |
if let textField = alertController.textFields?.first { | |
// 前後の空白文字を削除する要素を追加 | |
textField.text = textField.text!.trimmingCharacters(in: NSCharacterSet.whitespaces) | |
// 空白文字かどうかを確認する処理 | |
if textField.text != "" { | |
// todoListの配列に入力した値を先頭に挿入 | |
self.todoList.insert(textField.text!, at: 0) | |
// テーブルに行が追加されたことをデーブルに通知 | |
withRowAnimation: UITableViewRowAnimation.Right) | |
self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .right) | |
//-------------------- | |
// 保存処理を追加 | |
//-------------------- | |
let userDefaults = UserDefaults.standard | |
userDefaults.set(self.todoList, forKey: "todoList") | |
userDefaults.synchronize() | |
} | |
} | |
} | |
// OKボタンを追加 | |
alertController.addAction(okAction) | |
// キャンセルボタンがタップされたときの処理 | |
let cancelAction = UIAlertAction(title: "キャンセル", | |
style: UIAlertActionStyle.cancel, | |
handler: nil) | |
// キャンセルボタンを追加 | |
alertController.addAction(cancelAction) | |
// アラートダイアログを表示 | |
present(alertController, animated: true, completion: nil) | |
} | |
// func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { | |
// return true | |
// } | |
func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCellEditingStyle, | |
forRowAt indexPath: IndexPath) { | |
// 削除可能かどうか | |
if editingStyle == .delete { | |
// ToDoリストから削除 | |
todoList.remove(at: indexPath.row) | |
// セルを削除 | |
tableView.deleteRows(at: [indexPath as IndexPath], with: .fade) | |
//-------------------- | |
// 保存処理を追加 | |
//-------------------- | |
let userDefaults = UserDefaults.standard | |
userDefaults.set(self.todoList, forKey: "todoList") | |
userDefaults.synchronize() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment