プロファイル画面で、ユーザー名、メール ID、車のカテゴリのデータを firestore から取得します。ここでの車のカテゴリは、コンパクト、スモール、ミッドサイズ、フルです。また、carCategory パラメーター値を文字列として保存します (つまり、CarCategory: "1" は、車のカテゴリが"0"であることを意味し、車のカテゴリの値が"1"の場合は小型車であることを意味します)。
ユーザー更新ページでは、車のカテゴリはコレクション ビューです。フロー レイアウトはカルーセル ビュー
です。車のカテゴリのセル インデックス パスの値が 1 の場合、スクロールせずにセルの詳細が表示されます。
私の質問は、コレクション ビュー セルに特定のセル インデックスを読み込む方法です。
ここに私がこれまでに試したコードがあります::
**collection view and carousel view**
var car = [String]()
var carCategory = [ "Compact","small", "Midsize", "Full", "Van/Pick-up" ]
var carCategoryImage = ["compactCar", "smallCar", "mediumCar", "fullCar", "vanPickup"]
var carCategoryMeter = ["3.5 - 4.5m", "2.5 - 3.5m", "4 - 5m", "5 - 5.5m", "5.5 - 6.5m"]
var carCategoryLabel: UILabel?
var carMetersLabel: UILabel?
var carCategoryImageLabel: UIImageView?
var currentPageValue: String?
var currentCell: Int?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return carCategory.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! carCollectionSettingCollectionViewCell
cell.carName.text = carCategory[indexPath.row]
print("carcategoryIndex\(carCategory)")
cell.carImage.image = UIImage(named: carCategoryImage[indexPath.row])
cell.carMeters.text = carCategoryMeter[indexPath.row]
return cell
}
fileprivate var pageSize: CGSize {
let layout = self.carCollection.collectionViewLayout as! UPCarouselFlowLayout
var pageSize = layout.itemSize
if layout.scrollDirection == .horizontal {
pageSize.width += layout.minimumLineSpacing
} else {
pageSize.height += layout.minimumLineSpacing
}
return pageSize
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let layout = carCollection.collectionViewLayout as! UPCarouselFlowLayout
let pageSide = (layout.scrollDirection == .horizontal) ? self.pageSize.width : self.pageSize.height
let offset = (layout.scrollDirection == .horizontal) ? scrollView.contentOffset.x : scrollView.contentOffset.y
currentPage = Int(floor((offset - pageSide / 2) / pageSide) + 1)
print("currentpage::::\(currentPage)")
}
fileprivate var currentPage: Int! {
didSet {
currentPage = self.currentCell
print("currentapge::::\(currentPage)")
let character = self.carCategory[self.currentPage!]
print("character::::\(character)")
}
}
func loadUserData(){
API.User.observeCurrentUser { (user) in
if self.userName.text != nil {
self.userName.text = user.username
print("username:::\(String(describing: user.username))")
}
if let photoUrlString = user.profileImageURL {
let photoUrl = URL(string: photoUrlString)
self.profileImage.sd_setImage(with: photoUrl)
}
if self.email.text != nil {
self.email.text = user.email
}
switch user.carCategory {
case "0":
self.carCategoryLabel?.text = "Compact"
self.carMetersLabel?.text = "3.5 - 4.5m"
self.carCategoryImageLabel?.image = UIImage(named: "compactCar")
self.currentCell = 0
print("currentCell:::\(String(describing: self.currentCell))")
self.carCollection.reloadData()
case "1":
self.carCategoryLabel?.text = "Small"
self.carMetersLabel?.text = "2.5 - 3.5m"
self.carCategoryImageLabel?.image = UIImage(named: "smallCar")
self.carCategoryLabel?.text = "1"
self.currentCell = 1
print("currentCell:::\(String(describing: self.currentCell))")
self.carCollection.reloadData()
case "2":
self.carCategoryLabel?.text = "Midsize"
self.carMetersLabel?.text = "4 - 5m"
self.carCategoryImageLabel?.image = UIImage(named: "mediumCar")
self.currentCell = 2
print("currentCell:::\(String(describing: self.currentCell))")
self.carCollection.reloadData()
case "3":
self.carCategoryLabel?.text = "Full"
self.carMetersLabel?.text = "5 - 5.5m"
self.carCategoryImageLabel?.image = UIImage(named: "fullCar")
self.currentCell = 3
print("currentCell:::\(String(describing: self.currentCell))")
self.carCollection.reloadData()
case "4":
self.carCategoryLabel?.text = "Van/Pick-up"
self.carMetersLabel?.text = "5.5 - 6.5m"
self.carCategoryImageLabel?.image = UIImage(named: "vanPickup")
self.currentCell = 4
print("currentCell:::\(String(describing: self.currentCell))")
self.carCollection.reloadData()
default:
self.carCategoryLabel?.text = ""
self.carMetersLabel?.text = ""
self.carCategoryImageLabel?.image = UIImage(named: "")
break
}
}
}//loaduserData