画面遷移をする時にtextFieldの値を渡し、配列に追加してTableViewcellに表示させたいです。
具体的にはnikkitableViewControllerで『+』ボタンを押すと、hennsyuViewControllerに遷移し
そこでtextFieldに入力します。その後『完了』ボタンを押すことでnikkitableViewControllerにある配列に追加して、画面遷移しCellを生成したいです。
3画面構成になっているのですが、nikkitableViewController→ hennsyuViewController→ nikkitableViewControlleの流れの時だけ配列に追加したいです。
コードを書いてみたのですがCellが生成されず困っています。
文字数の都合で2番目の画面コード以下に記載します。
swift
1import UIKit 2 3class nikkiTableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{ 4 5 private let cellId = "cellId" 6 7 var addBarButtonItem:UIBarButtonItem! 8// var tableViewCell = nikkiTableViewCell() 9 10 var topImageArray = [UIImage]() 11 var nameArray = [String]() 12 var dateArray = [String]() 13 var hyoukaArray = [String]() 14 var textViewArray = [String]() 15 16 @IBOutlet weak var nikkiTableView: UITableView! 17 18 19 override func viewDidLoad() {super.viewDidLoad() 20 21 setupSearchBar() 22 23 addBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addBarButtonTapped(_:))) 24 addBarButtonItem.tintColor = UIColor.white 25 26 //navigationVarの右側に追加 27 self.navigationItem.rightBarButtonItem = addBarButtonItem 28 29 nikkiTableView.delegate = self 30 nikkiTableView.dataSource = self 31 32 let appearance = UINavigationBarAppearance() 33 appearance.configureWithOpaqueBackground() 34 appearance.backgroundColor = .rgb(red: 173, green: 216, blue: 230) 35 36 //標準の高さ 37 navigationItem.standardAppearance = appearance 38 //ナビゲーションバーが透過する 39 navigationItem.scrollEdgeAppearance = appearance 40 //標準の文字の大きさ 41 navigationItem.compactAppearance = appearance 42 43 let tapGesture = UITapGestureRecognizer( 44 target: self, 45 action: #selector(dismissKeyboard)) 46 view.addGestureRecognizer(tapGesture) 47 48 tapGesture.cancelsTouchesInView = false 49 50 51 } 52 53 54 func setupSearchBar(){ 55 56 if let navigationBarFrame = navigationController?.navigationBar.bounds { 57 let searchBar: UISearchBar = UISearchBar(frame: navigationBarFrame) 58 searchBar.delegate = self 59 searchBar.placeholder = "タイトルで探す" 60 searchBar.tintColor = UIColor.white 61 searchBar.keyboardType = UIKeyboardType.default 62 navigationItem.titleView = searchBar 63 navigationItem.titleView?.frame = searchBar.frame 64 65 } 66 } 67 68 69 70 @objc func dismissKeyboard() { 71 navigationItem.titleView?.endEditing(true) 72 } 73 74 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 75 searchBar.resignFirstResponder() 76 } 77 78 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 79 80 return 190 81 } 82 83 func numberOfSections(in tableView: UITableView) -> Int { 84 85 return 1 86 } 87 88 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 89 90 return nameArray.count 91 92 } 93 94 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 95 96 let cell = nikkiTableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! nikkiTableViewCell 97 98 //cell.topImageView?.image = topImageArray[indexPath.row] 99 cell.nameLabel?.text = nameArray[indexPath.row] 100 cell.dateLabel?.text = dateArray[indexPath.row] 101 cell.hyoukaLabel?.text = hyoukaArray[indexPath.row] 102 103 104 return cell 105 106 } 107 108 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 109 110 let storyboard = UIStoryboard(name: "nikkiTableView", bundle: nil) 111 let textVC = storyboard.instantiateViewController(withIdentifier: 112 "textViewController") 113 //値、参照渡しのコードを書く 114 navigationController?.pushViewController(textVC, animated: true) 115 //タップした後、セルの色が白色に戻る 116 tableView.deselectRow(at: indexPath, animated: true) 117 118 navigationItem.titleView?.endEditing(true) 119 120 121 } 122 123 124 @objc func addBarButtonTapped(_ sender: UIBarButtonItem) { 125 126 let hennsyuVc = storyboard?.instantiateViewController(withIdentifier: "hennsyuViewController") as! hennsyuViewController 127 navigationController?.pushViewController(hennsyuVc, animated: true) 128 129 } 130 131 override func viewWillAppear(_ animated: Bool) { 132 133 nikkiTableView.reloadData() 134 135 // TODO: ①に戻ってきたらUITableViewを再描画します。 136 if AppState.isEdited { 137 // ③から完了をタップして戻ってきた場合は②に遷移します。 138 AppState.isEdited = false 139 let textvc = storyboard?.instantiateViewController(withIdentifier: "textViewController") as! textViewController 140 141 //textvc.nameField = nameArray 142 143 navigationController?.pushViewController(textvc, animated: true) 144 145 } 146 147 } 148 149} 150 151 152 class nikkiTableViewCell: UITableViewCell { 153 154 @IBOutlet weak var topImageView: UIImageView! 155 @IBOutlet weak var nameLabel: UILabel! 156 @IBOutlet weak var dateLabel: UILabel! 157 @IBOutlet weak var hyoukaLabel: UILabel! 158 159 override func awakeFromNib() { 160 super.awakeFromNib() 161 162} 163 override func setSelected(_ selected: Bool, animated: Bool) { 164 super.setSelected(selected, animated: animated) 165 166 } 167 168 169}
import UIKit
import DKImagePickerController
class hennsyuViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate, UITextViewDelegate {
var completButtonItem:UIBarButtonItem!
@IBOutlet weak var textField: UITextView!
@IBOutlet weak var dateField: UITextField!
@IBOutlet weak var hyoukaField: UITextField!
@IBOutlet weak var addressField: UITextField!
@IBOutlet weak var nameField: UITextField!
var photos: [UIImage] = []
// 実際に選択された枚数
var selectedCount = 0
let pickerController = DKImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
dateField.delegate = self
hyoukaField.delegate = self
addressField.delegate = self
nameField.delegate = self
textField.delegate = self
// checkpermission.checkCamera()
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .rgb(red: 173, green: 216, blue: 230)
navigationController?.navigationBar.tintColor = UIColor.white
//標準の高さ
navigationItem.standardAppearance = appearance
//ナビゲーションバーが透過する
navigationItem.scrollEdgeAppearance = appearance
//標準の文字の大きさ
navigationItem.compactAppearance = appearance
completButtonItem = UIBarButtonItem(title: "完了", style: .done, target: self, action: #selector(completButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = completButtonItem
dateField.setUnderLine()
hyoukaField.setUnderLine()
addressField.setUnderLine()
nameField.setUnderLine()
let toolBar = UIToolbar()
toolBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40)
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let kbDoneButton = UIBarButtonItem(title: "閉じる", style: .done, target: self, action:#selector(kbDoneTaped) )
toolBar.items = [spacer,kbDoneButton]
textField.inputAccessoryView = toolBar
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(hennsyuViewController.keyboardShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(hennsyuViewController.keyboardHide(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
}
@IBAction func addImageButton(_ sender: Any) {
pickerController.maxSelectableCount = 5
//カメラモード、写真モードの選択
pickerController.sourceType = .photo
//キャンセルボタンの有効化
pickerController.showsCancelButton = true
//pickerController.UIDelegate = CustomUIDelegate()
pickerController.didSelectAssets = { (assets: [DKAsset]) in print("didSelectAssets"); print(assets)
//assetsのカウントがnilでないとき実行
// if assets.count > 0 {
//
// }
}
pickerController.modalPresentationStyle = .fullScreen
present(pickerController, animated: true, completion: nil)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
dateField.resignFirstResponder()
hyoukaField.resignFirstResponder()
addressField.resignFirstResponder()
nameField.resignFirstResponder()
return true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
self.view.endEditing(true)
}
@objc func keyboardShow(_ notification: Notification){
if !textField.isFirstResponder {
return
}
if self.view.frame.origin.y == 0 {
if let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y -= keyboardRect.height
}
}
}
@objc func keyboardHide(_ notification: Notification){
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
@objc func kbDoneTaped (_ sender:UIButton){
self.view.endEditing(true)
}
//完了ボタンを押した時
@objc func completButtonTapped(_ sender: UIBarButtonItem){
if let n = navigationController,
n.viewControllers[n.viewControllers.count - 2] is nikkiTableViewController {
// ①から遷移してきた場合は完了をタップしたことを表すフラグを設定します。
AppState.isEdited = true
}
navigationController?.popViewController(animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
if AppState.isEdited {
let nikkiVC = storyboard?.instantiateViewController(withIdentifier: "nikkiTableViewController") as! nikkiTableViewController
nikkiVC.nameArray.append(nameField.text!)
nikkiVC.dateArray.append(nameField.text!)
nikkiVC.hyoukaArray.append(hyoukaField.text!)
nikkiVC.textViewArray.append(textField.text!)
}
}
}
回答1件
あなたの回答
tips
プレビュー