如何从UITableViewCell(Swift)中嵌入的UICollectionView单元格中进行分离?
我曾经成功地将UICollectionView嵌入到ViewController中的UITableView中 如果您快速查看链接教程的代码,再加上它也是一件很好的学习内容,那么下面的内容可能会更有意义 我的下一步是从tableView的UITableViewCells中的UICollectionView单元格执行分段,但由于collectionView出口是在单独的视图控制器中建立的,我不确定如何在主视图控制器中引用它 在TableViewCell.swift中有:如何从UITableViewCell(Swift)中嵌入的UICollectionView单元格中进行分离?,swift,uitableview,uicollectionview,segue,Swift,Uitableview,Uicollectionview,Segue,我曾经成功地将UICollectionView嵌入到ViewController中的UITableView中 如果您快速查看链接教程的代码,再加上它也是一件很好的学习内容,那么下面的内容可能会更有意义 我的下一步是从tableView的UITableViewCells中的UICollectionView单元格执行分段,但由于collectionView出口是在单独的视图控制器中建立的,我不确定如何在主视图控制器中引用它 在TableViewCell.swift中有: class TableVie
class TableViewCell: UITableViewCell {
@IBOutlet private weak var collectionView: UICollectionView!
}
extension TableViewCell {
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.reloadData()
}
}
“object”在SecondViewController中实现,如下所示,var object:PFObject
我现在需要用collectionView来填补上面代码中的空白,以便在SecondViewController中显示正确的“对象”,destinationViewController我刚刚使用了您附加的代码。我在故事板中手动添加了segue。打电话很容易 1只需将viewcontroller添加到情节提要。 2控制+从collectionview单元格拖放序列
在代码中,对CollectionView的引用在TableViewCell中连接。 所以,您可以通过TableViewCell引用它,并设置datasource和delegate。 要将连接添加到代码中,您必须使用RightButton选择CollectionView,然后拖放到@IBOutlet private-var-CollectionView:UICollectionView! 当单击CollectionViewCell、collectionViewcollectionView:UICollectionView时,UICollectionViewDelegate的DIDSelectedEditeMatIndexPath indexPath:NSIndexPath函数将被委托调用。 此时,您可以通过在此函数中调用performsgue进行分段。 在编写代码时,tableview的索引是按标记设置的,因此可以使用collectionView.tag按标记获取索引 代码如下所示。 var tableViewIndex=collectionView.tag 因此,可以使用tableViewIndex和indexPath引用正确的单元格
嗨,我也有同样的问题 这就是我所做的: 一,。在第一个视图控制器的扩展中为单元设置标记 二,。在情节提要上执行从UIcollectionViewCell到新视图控制器的顺序 三,。在第一个TableViewController的prepareForSegue中: 从UICollectionViewCell将推送序列从IB添加到ViewController。 为您的segue YourSegueIdentifier指定一个标识符。 在自定义UITableViewController或UIViewController中,重写prepareForSegue方法。 这是:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueIdentifier" {
if let collectionCell: YourCollectionViewCell = sender as? YourCollectionViewCell {
if let collectionView: UICollectionView = collectionCell.superview as? UICollectionView {
if let destination = segue.destination as? YourViewController {
// Pass some data to YourViewController
// collectionView.tag will give your selected tableView index
destination.someObject = tableObjects[collectionView.tag].someObject
destination.productId = collectionCell.product?.id
}
}
}
}
}
您好,这很好,但我确实需要能够引用collectionView插座本身,因为我需要能够将数据从单元传输到目标视图控制器。我应该在最初的问题中说清楚对不起!我是否能够从didSelectItemAtIndexPath内部的解析后端提取的数组中准备此数据?因为现在它一直在吐错误,这是可能的。您没有在ViewConroller中实现UICollectionView的委托和数据源吗?我认为您可以在ViewController中实现UITableViewDelegate和UITableViewDatasource,并使用setCollectionViewDataSourceDelegate。如果是这样,单击单元格时将调用didselectitematindexpath。重要的是,您必须在ViewController中实现UICollectionViewDelegate和UICollectionViewDatasource,并使用setCollectionViewDataSourceDelegate进行设置。是的,这部分很好,但我需要在prepareForSegue中引用目标视图控制器的出口以显示正确的数据。为了更好地解释这一点,我更新了我的问题。谢谢你迄今为止的帮助
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var tableViewIndex = collectionView.tag;
self.performSegueWithIdentifier("segueIdentifier", sender: self)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("advertisementCollectionCell",forIndexPath: indexPath) as! AdvertisementCollectionViewCell
// set your cell information
cell.tag = indexPath.row
return cell
}
else if segue.identifier == "identifier"
{
let destinationViewController = segue.destinationViewController as! DestinationViewController
if let cell = sender as? MyCollectionViewCell
{
let indexPath = cell.tag
// use indexPath :D
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueIdentifier" {
if let collectionCell: YourCollectionViewCell = sender as? YourCollectionViewCell {
if let collectionView: UICollectionView = collectionCell.superview as? UICollectionView {
if let destination = segue.destination as? YourViewController {
// Pass some data to YourViewController
// collectionView.tag will give your selected tableView index
destination.someObject = tableObjects[collectionView.tag].someObject
destination.productId = collectionCell.product?.id
}
}
}
}
}