我目前有一個UICollectionView
表與單元格,我試圖讓每個單元格被創建有自己獨特的視圖控制器。例如,當點擊UICollectionViewCell
時,視圖控制器將顯示該特定單元格。我知道我可以創建一個viewcontroller並執行segue只有一個視圖控制器。這隻涵蓋了一個單元格...如果用戶創建了25個單元格......我如何爲每個單元格創建視圖控制器而無需創建一個segue?下面的代碼是創建一個單元格。創建新的視圖控制器UICollectionViewCell點擊
// MARK: Create collection View cell with title, image, and rounded border
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ChatCell
let object = objects[indexPath.row]
cell.chatLabel.text = object.title ?? ""
cell.chatImage.image = object.image
if let chatImagePath = object.imagePath {
if let imageURL = URL(string: chatImagePath) {
cell.chatImage.sd_setImage(with: imageURL)
}
}
cell.layer.borderWidth = 0.5
cell.layer.borderColor = UIColor.darkGray.cgColor
cell.layer.masksToBounds = true
cell.layer.cornerRadius = 8
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return objects.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth = photoCollectionView.bounds.width
let itemHeight = photoCollectionView.bounds.height/2
return CGSize(width: itemWidth, height: itemHeight)
}
但是,您是否真的有這些目標視圖控制器的25個完全不同的設計?或者他們是同一種基本類型的視圖控制器,只是顯示不同的數據? – Rob
@Rob我想爲該單元格創建一個視圖控制器...我不能讓所有的單元格都指向相同的視圖控制器 –
@Rob是他們都有相同的佈局,但我想發佈不同的數據在每個 –