【发布时间】:2021-11-19 10:47:24
【问题描述】:
@TusharMordiya Please check this image 我在 CollectionView 中创建了一个 tableView。视图的内容是 UIImage 和 UILabel。我想设计一个单元格,当我单击一个单元格时,图像和标签必须转到另一个 ViewController。
import UIKit
class ExploreTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
@IBOutlet var collectionView: UICollectionView!
var namearr = ["images-1", "images-2", "images-3", "images-4", "images-5"]
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 132, height: 134)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UIStoryboard(name: "DetailViewController", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController
vc?.name = namearr[indexPath.row]
vc?.img.image = UIImage(named: namearr[indexPath.row])
//self.navigationController?.pushViewController(vc, animated: true)
// showing error in here and while declaring object of viewcontroller
}
}
【问题讨论】:
-
我猜你可能想解开
vc。但是显示您拥有的完整错误消息可能会有所帮助。我们可以帮助您理解它。我猜到了错误,但它可能是别的东西...... -
我更仔细地阅读了您的代码(如果您显示错误消息,我就不需要了)。但是你在 UITableViewCell 中有一个 UICollectionView,所以这里的
self是UITableViewCell,而不是UIViewController。您需要用closure或delegate系统告诉UIViewController,以使用相应的数据推送新的视图控制器。 -
我会质疑你为什么在 UITableViewCell 中有一个 UICollectionView?您是否尝试在表格视图中创建水平滚动行?如果是这种情况,我建议根本不使用 UITableView 并查看 UICollectionViewCompositionalLayout...developer.apple.com/documentation/uikit/…
-
@Larme 我在 didSelectItemAt indexPath 和实例化故事板中有错误
-
真正的错误信息是什么?重要的是显示真实信息,而不是“伪信息”。
标签: ios swift uicollectionview pushviewcontroller