collectionViewにてcell毎に処理をする方法
現在コレクションビューでリストアプリを作っています。
セルをクリックした遷移先(inputViewController)で入力した情報を、コレクションビューのセル上のラベルに表示したいのですが、入力した情報が全てのセルに反映されてしまい困っています。
遷移先のテキストフィールドで取得した日時を遷移元(viewContoroller)に表示したいです。
画面の遷移はstoryboad上のsegueにて、コレクションビューセルからinputviewcontrollerに繋いでいます。
選択されたセル毎に反映させる方法をご存知でしたら教えていただけたら嬉しいです。
はじめての質問なので不慣れな点もあるかと思いますがどうかご教示のほどお願いいたします。
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
//コレクションビューのレイアウト
@IBOutlet weak var collectionView: UICollectionView!
var text1: String?
let toDos = ["1","2","3","4","5","6"]
private let itemsPerRow: CGFloat = 3
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 100 , height: 100)
collectionView.collectionViewLayout = layout
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6//表示するセルの数
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
//セルの色
cell.backgroundColor = .white
let label = cell.contentView.viewWithTag(2) as! UILabel
label.text = toDos[indexPath.row]
let dateLabel = cell.contentView.viewWithTag(3) as! UILabel
//dateLabel.text = text1[indexPath.row]
dateLabel.text = text1
return cell
}
func collectionview(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let horizontalSpace : CGFloat = 20
let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace
return CGSize(width: cellSize, height: cellSize)
}
}
import UIKit
class inputViewController: UIViewController,UITextFieldDelegate{
@IBOutlet weak var textField: UITextField!
var datePicker: UIDatePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
datePicker.datePickerMode = UIDatePicker.Mode.date
datePicker.timeZone = NSTimeZone.local
datePicker.locale = Locale(identifier: "ja")
textField.inputView = datePicker
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: #selector(done))
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done)); toolBar.setItems([spaceItem,doneItem],animated: true)
textField.inputView = datePicker
textField.inputAccessoryView = toolBar
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
@ objc func done(){
textField.endEditing(true)
let formatter = DateFormatter()
formatter.dateFormat = "yyyy年MM月dd日"
textField.text = "\(formatter.string(from: datePicker.date))"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func finishButton(_ sender: Any) {
let viewController: ViewController =
self.storyboard?.instantiateViewController(withIdentifier: "finish") as! ViewController
viewController.text1 = textField.text
self.navigationController?.pushViewController (viewController, animated: true)
}
}
import UIKit
protocol inputViewControllerDelegate: class {
func inputViewController(_ inputVC: inputViewController, didFinishWithText text: String?)
}
class inputViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
weak var delegate: inputViewControllerDelegate?
var datePicker: UIDatePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
datePicker.datePickerMode = UIDatePicker.Mode.date
datePicker.timeZone = NSTimeZone.local
datePicker.locale = Locale(identifier: "ja")
textField.inputView = datePicker
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: #selector(done))
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done)); toolBar.setItems([spaceItem,doneItem],animated: true)
textField.inputView = datePicker
textField.inputAccessoryView = toolBar
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
@ objc func done(){
textField.endEditing(true)
let formatter = DateFormatter()
formatter.dateFormat = "yyyy年MM月dd日"
textField.text = "\(formatter.string(from: datePicker.date))"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func finishBtn(_ sender: Any){
delegate?.inputViewController(self, didFinishWithText: textField.text)
self.dismiss(animated: true, completion: nil)
}
}