【发布时间】:2020-06-05 03:45:28
【问题描述】:
下面是包含 CollectionView 及其属性的 Triggered viewController:
import UIKit
struct CellModel {
let imgImage: UIImage
let carNumber: String
let locationInfo: String
let modifiedDate: String
let modifiedBy: String
}
class MerchandizeEntranceViewController: UIViewController {
let data: [CellModel] = [CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim")]
let idCell = "idCell"
@IBOutlet weak var customCollectionView: UICollectionView!
@IBOutlet weak var addButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .cyan
customCollectionView?.dataSource = self
customCollectionView?.delegate = self
customCollectionView?.register(CustomCell.self, forCellWithReuseIdentifier: idCell)
}
}
class CustomCell: UICollectionViewCell {
@IBOutlet weak var imgImage: UIImageView!
@IBOutlet weak var carNumber: UILabel!
@IBOutlet weak var locationInfo: UILabel!
@IBOutlet weak var modifiedDate: UILabel!
@IBOutlet weak var modifiedBy: UILabel!
public func configModel(with model: CellModel ) {
imgImage?.image = model.imgImage
carNumber?.text = model.carNumber
locationInfo?.text = model.locationInfo
modifiedDate?.text = model.modifiedDate
modifiedBy?.text = model.modifiedBy
}
}
extension MerchandizeEntranceViewController: UICollectionViewDelegate {
}
extension MerchandizeEntranceViewController: UICollectionViewDelegateFlowLayout {
private func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 384, height: 100)
}
}
extension MerchandizeEntranceViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: idCell, for: indexPath) as! CustomCell
cell.configModel(with: data[indexPath.row])
return cell
}
}
下面是“didSelectItemAt indexpath:”在我的 mainViewController 中调用的方法,它触发了DetailedViewcontrolers(上面的 ViewController 内容)显示了它们的内容:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell_Menu = collectionView.dequeueReusableCell(withReuseIdentifier: "menuCell", for: indexPath) as? MenuCell {
switch indexPath.row {
case 0:
self.performSegue(withIdentifier: "segue0", sender: AnyClass.self)
case 1:
self.performSegue(withIdentifier: "segue1", sender: AnyClass.self)
case 2:
self.performSegue(withIdentifier: "segue2", sender: AnyClass.self)
case 3:
self.performSegue(withIdentifier: "segue3", sender: AnyClass.self)
case 4:
self.performSegue(withIdentifier: "segue4", sender: AnyClass.self)
case 5:
self.performSegue(withIdentifier: "segue5", sender: AnyClass.self)
case 6:
self.performSegue(withIdentifier: "segue6", sender: AnyClass.self)
default: NSLog("no view is defined for this cell")
}
} else {
if let cell_Story = collectionView.dequeueReusableCell(withReuseIdentifier: "storyCell", for: indexPath) as? StoryCell {
switch indexPath.row {
case 0:
NSLog("No addidtional view defined for story cell yet.. .")
default: break
}
}
}
}
如果这段代码流有任何问题,任何人都可以查看并纠正我..肯定有问题..
【问题讨论】:
-
您检查过您的 IBOutlet 吗?有没有 UICollectionView 的引用
-
当然,我做到了……它有参考……所有连接..
-
首先注册 1. customCollectionView?.register(CustomCell.self, forCellWithReuseIdentifier: idCell) 2. customCollectionView?.dataSource = self customCollectionView?.delegate = self 3. customCollectionView?.reloadData() //虽然您的情况不需要它
-
不相关但将
cellForRowAt之外的单元格出列是没有意义的,特别是该单元格无论如何都未使用。奇怪的参数AnyClass.self是什么?如果您使用的是原型单元格,则不得注册这些单元格。 -
@vadian,大声笑.. 是的,我知道这很奇怪,但其他集合 View 的单元格也被按下并触发到同一个 viewController,这使它看起来更奇怪.. 可以说,我得到了2个具有不同单元格数量的不同集合视图,如果我不使用“出队”两个集合视图中的两个单元格,即使它们各自具有不同的内容,它们也会继续显示相同的内容..这就是为什么我暂时将其保留在那里以首先检查是否意见正在发挥作用...... )
标签: ios swift xcode uicollectionview