Ios 如何从TableViewCell上的自定义CollectionViewCell推送VC?
我有一个Ios 如何从TableViewCell上的自定义CollectionViewCell推送VC?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个tableView和单元格,在单元格上我有一个collectionView并在其上显示一些内容 我想发送一个关于选择indepath的链接 我想从TableViewCell上的自定义CollectionViewCell推送/显示我的视图 class secondTopicTableViewCell: UITableViewCell { @IBOutlet weak var relatedCustom: UICollectionView! var relArray = N
tableView
和单元格,在单元格上我有一个collectionView
并在其上显示一些内容
我想发送一个关于选择indepath
的链接
我想从TableViewCell
上的自定义CollectionViewCell
推送/显示我的视图
class secondTopicTableViewCell: UITableViewCell {
@IBOutlet weak var relatedCustom: UICollectionView!
var relArray = NSArray()
func loadArray(arr: NSArray) {
self.relArray = arr
self.relatedCustom.reloadData()
}
}
extension secondTopicTableViewCell : UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return relArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collection", forIndexPath: indexPath) as! relatedCollectionViewCell
let info = self.relArray.objectAtIndex(indexPath.row) as! specificTopicInfo
cell.showInfo(info)
return cell
}
}
extension secondTopicTableViewCell : UICollectionViewDelegate {
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let relatedTopic = self.relArray.objectAtIndex(indexPath.row) as! specificTopicInfo
let str = relatedTopic.relatedLink!
print(str)
}
}
class relatedCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var relatedLabel: UILabel!
func showInfo(info: specificTopicInfo) {
relatedLabel.backgroundColor = UIColor.grayColor()
relatedLabel.text = info.relatedTitle
}
}
您只需要使用与Tableview控件相同的didSelectItemAtIndexPath进行导航。
将导航代码写入Collectionview的didSelectItemAtIndexPath如果将集合视图嵌套在表视图单元格中,并希望从显示第一个单元格的表视图触发操作,可以执行两件事 弗斯特 您可以使用UICollectionViewDelegate协议捕获用户与集合视图单元格的交互 不要忘记将表视图单元格设置为其集合视图的委托 第二 您可以创建自己的协议(当单元格有多个按钮时很有用)
然后,每个按钮都将有自己的“didTap”方法,而不是原始集合视图委托中的“didSelect”方法。当我在didSelectItemAtIndexPath中编写代码时,它表示TableViewCell没有单元格类的Mamber请参阅:Why
class secondTopicTableViewCell:UITableViewCell{class secondTopicTableViewCell:UITableViewCell{
?两次!@Mr.UB这是个错误,抱歉。