前提・実現したいこと
collectionViewを使ったアプリで、アプリを立ち上げたときだけcellが表示されません。
ほかの画面に遷移してから戻ると表示されます。
該当のソースコード
Swift
1import UIKit 2import FirebaseAuth 3import FirebaseFirestore 4import PKHUD 5 6class ViewController: UIViewController { 7 8 private var user: User? 9 private var tickets = [Ticket]() 10 let cellId = "ticketCellId" 11 12 @IBOutlet weak var headerLabel: UILabel! 13 @IBOutlet weak var headerView: UIView! 14 @IBOutlet weak var ticketCollectionView: UICollectionView! 15 @IBOutlet weak var addTicketButton: UIButton! 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 setupLayout() 21 22 } 23 24 private func setupLayout() { 25 view.backgroundColor = UIColor(red: 241/255, green: 243/255, blue: 246/255, alpha: 1.0) 26 27 addTicketButton.tintColor = .red 28 addTicketButton.setTitle("", for: .normal) 29 addTicketButton.setImage(UIImage(systemName: "rectangle.badge.plus"), for: .normal) 30 addTicketButton.addTarget(self, action: #selector(tappedAddTicketButton), for: .touchUpInside) 31 32 headerView.backgroundColor = UIColor(red: 241/255, green: 243/255, blue: 246/255, alpha: 1.0) 33 34 ticketCollectionView.dataSource = self 35 ticketCollectionView.delegate = self 36 ticketCollectionView.register(UINib(nibName: "TicketCell", bundle: nil), forCellWithReuseIdentifier: cellId) 37 } 38 39 override func viewWillAppear(_ animated: Bool) { 40 super.viewWillAppear(animated) 41 42 guard let uid = Auth.auth().currentUser?.uid else { return } 43 Firestore.fetchUserFromFirestore(uid: uid) { (user) in 44 if let user = user { 45 self.user = user 46 self.headerLabel.text = "(user.name)のチケット" 47 } 48 } 49 fetchTickets() 50 ticketCollectionView.reloadData() 51 } 52 53 @objc private func tappedAddTicketButton() { 54 55 if Auth.auth().currentUser?.uid == nil { 56 let storybord: UIStoryboard = UIStoryboard(name: "Register", bundle: nil) 57 let vc = storybord.instantiateViewController(withIdentifier: "Register") as! RegisterViewController 58 let nav = UINavigationController(rootViewController: vc) 59 nav.modalPresentationStyle = .fullScreen 60 self.present(nav, animated: true, completion: nil) 61 } else { 62 let nextStoryboad: UIStoryboard = UIStoryboard(name: "AddTicket", bundle: nil) 63 let AddTicketController = nextStoryboad.instantiateViewController(withIdentifier: "AddTicket") as! AddTicketViewController 64 self.present(AddTicketController, animated: true, completion: nil) 65 } 66 } 67 68 private func fetchTickets() { 69 guard let uid = Auth.auth().currentUser?.uid else { return } 70 Firestore.fetchTicketFromFirebase(uid: uid) { (tickets) in 71 self.tickets = tickets 72 print("チケットの取得に成功") 73 } 74 } 75 76} 77 78extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 79 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 80 return tickets.count 81 } 82 83 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 84 let cell = ticketCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! TicketCell 85 cell.layer.cornerRadius = 15 86 cell.backgroundColor = .white 87 cell.ticketItem = tickets[indexPath.row] 88 return cell 89 } 90 91 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 92 let horizontalSpace: CGFloat = 30 93 let cellSize: CGFloat = self.view.frame.width / 2 - horizontalSpace 94 return .init(width: cellSize, height: cellSize * 1.2) 95 } 96}
試したこと
cellのカウントのところを数字にしたら表示されました。
tickets.countにすると表示されません。
回答1件
あなたの回答
tips
プレビュー