[英]How to set collection view cell of specific index in iOS Swift?
在配置文件屏幕中,從用戶名,電子郵件ID,汽車類別的Firestore檢索數據。 這里的汽車類別為緊湊型,小型,中型和全尺寸。 並將carCategory參數值存儲為String(即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
下面的代碼可能會幫助您解決此問題
// MARK: - Collectionview delegates
extension FaqcategoriesViewController : UICollectionViewDelegate, UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (Global.shared.FAQsCategoriesList?.list.count)!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "faqcategoriesCollectioncell",
for: indexPath) as! FAQCategoryCollectionViewCell
cell.configure(data: (Global.shared.FAQsCategoriesList?.list[indexPath.row])!)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyBoard = Utilities.getStoryboard(name: SBMEMBERSANDCOMMISSIONS)
let nextView = storyBoard.instantiateViewController(withIdentifier: "faq") as! FAQViewController
nextView.id = indexPath.row
self.navigationController?.pushViewController(nextView, animated: false)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let collectionViewSize = collectionView.frame.size.width
print(self.view.frame.width)
print(collectionView.frame.size.width)
print( "width: \(collectionViewSize/2 - 20) height: \(collectionView.frame.size.height/3.3)")
return CGSize(width: collectionViewSize/2 - 5, height: collectionView.frame.size.height/3.3)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
}
這是可以做到的:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.selectItem(at: [0, currentCell], animated: false, scrollPosition: .centeredHorizontally)
}
當您加載viewController時,我假設您設置了currentCell? 我看不到您原來的viewDidLoad()
或viewDidAppear()
函數。 如果從先前的viewController實例化此viewController,則從那里設置currentCell。 如果您選擇使用此viewController,請在prepareForSegue
方法中進行設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.