swift
1import UIKit 2import RealmSwift 3 4extension UIColor { 5 class func rgb(r: Int, g: Int, b: Int, alpha: CGFloat) -> UIColor{ 6 return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha) 7 } 8} 9 10class TopViewViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 11 12 13 @IBOutlet weak var TopViewTabelView: UITableView! 14 15 var isSlidemenuHidden = true 16 17 @IBOutlet weak var sidemenuconstant: UIView! 18 @IBOutlet weak var sideMLabel: UILabel! 19 @IBOutlet weak var sideTableView: UITableView! 20 21 let side = ["No.1","No2","No3"] 22 23 let ToDo = TopTodo() 24 var todoItem:Results<TopTodo>! 25 26 27 28 override func viewDidLoad() { 29 super.viewDidLoad() 30 31 sidemenuconstant.isHidden = true 32 33 34 navigationItem.title = "履歴" 35 // 永続化されているデータを取りだす 36 let realm = try! Realm() 37 self.todoItem = realm.objects(TopTodo.self) 38 self.TopViewTabelView.reloadData() 39 40 41 //navigationController?.navigationBar.prefersLargeTitles = true //これがやりたかった設定やメモ帳のナビゲーションの可変のやつ 42 navigationItem.title = "データベース" 43 TopViewTabelView.tableFooterView = UIView(frame: .zero) 44 self.view.backgroundColor = UIColor.rgb(r: 250, g: 204, b: 125, alpha: 1) 45 TopViewTabelView.frame = CGRect(x: 0, y: 64, width: 375, height: 667) 46 47 } 48 49 50 51 52 @IBAction func addBtr(_ sender: Any) { 53 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 54 55 // OKボタンの設定 56 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 57 (action:UIAlertAction!) -> Void in 58 59 // OKを押した時入力されていたテキストを表示 60 if let textFields = alert.textFields { 61 62 // アラートに含まれるすべてのテキストフィールドを調べる 63 for textField in textFields { 64 //self.item.insert(textField.text!, at: 0) 65 66 let newTodo = TopTodo() 67 newTodo.item = textField.text! 68 69 let realm = try! Realm() 70 71 // トランザクション開始 72 try! realm.write({ () -> Void in 73 realm.add(newTodo) 74 print("ToDo Saved") 75 }) 76 // ここでトランザクションが終了 77 78 // ここでtodoItemの更新 79 self.todoItem = realm.objects(TopTodo.self) 80 self.TopViewTabelView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 81 print(textField.text!) 82 83 } 84 self.TopViewTabelView.reloadData() 85 } 86 }) 87 alert.addAction(okAction) 88 89 // キャンセルボタンの設定 90 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 91 alert.addAction(cancelAction) 92 93 // テキストフィールドを追加 94 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 95 textField.placeholder = "テキスト" 96 }) 97 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 98 99 self.present(alert, animated: true, completion: nil) 100 } 101 102 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 103 return todoItem.count 104 return side.count 105 } 106 107 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 108 let Cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 109 110 let object = todoItem[indexPath.row] 111 let now = NSDate()//object.date// // 現在日時の取得 112 113 114 115 let dateFormatter = DateFormatter() 116 dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale? // ロケールの設定 117 dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定 118 119 let dateString = dateFormatter.string(from: now as Date) 120 print(dateString) // -> 2014/06/25 02:13:18*/ 121 122 123 Cell.textLabel?.text = object.item 124 Cell.detailTextLabel?.text = dateString 125 print(object.date) 126 127 return Cell 128 129 let cell = tableView.dequeueReusableCell(withIdentifier: "impotant", for: indexPath) 130 cell.textLabel?.text = side[indexPath.row] 131 return cell 132 } 133 134 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 135 136 137 if(editingStyle == UITableViewCellEditingStyle.delete) { 138 139 let realm = try! Realm() 140 try! realm.write { 141 realm.delete(self.todoItem[indexPath.row]) 142 } 143 TopViewTabelView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade) 144 TopViewTabelView.reloadData() 145 } 146 } 147 148 @IBAction func seidemenu(_ sender: Any) { 149 150 if isSlidemenuHidden{ 151 sidemenuconstant.isHidden = false 152 }else{ 153 sidemenuconstant.isHidden = true 154 } 155 } 156 157 158 159}
実現したい事
サイドメニューをボタンをタップ時に表示させ、サイドメニュー以外をタップ時に隠れるようにしたい。
できている事
サイドメニューをViewで作りその中にtableView
を入れました。
そして、サイドメニューをデフォルト時に表示させない為にishidden
で隠しました。
###困っている事
ボタンをタップ時に表示されなく困っています。
また、一つのViewcontroller
に二つのtableView
を入れた時の処理のコーディングが分からなくて困っています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/27 15:15