Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 性能分段';UITableViewCell类中的_Ios_Swift_Uitableview_Uicollectionview_Uistoryboardsegue - Fatal编程技术网

Ios 性能分段';UITableViewCell类中的

Ios 性能分段';UITableViewCell类中的,ios,swift,uitableview,uicollectionview,uistoryboardsegue,Ios,Swift,Uitableview,Uicollectionview,Uistoryboardsegue,我有TableView并且在TableView单元格中添加了UICollectionView。现在,当用户点击任何UICollectionView时,我想将数据传递到下一个viewController,但不幸的是,我无法在UITableViewCell类中使用perform segue,因为我没有选择项。有没有办法在这里表演segue 整个代码 class UserLibraryViewController: UIViewController { extension UserLibraryVi

我有
TableView
并且在
TableView单元格中添加了
UICollectionView
。现在,当用户点击任何
UICollectionView
时,我想将数据传递到下一个
viewController
,但不幸的是,我无法在UITableViewCell类中使用perform segue,因为我没有选择项。有没有办法在这里表演segue

整个代码

class UserLibraryViewController: UIViewController {

extension UserLibraryViewController : UITableViewDelegate { }

extension UserLibraryViewController : UITableViewDataSource {

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return myBooksGenre[section]
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return myBooksGenre.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell
    cell.tableIndexPath = indexPath

    return cell
}
}
TableViewCell

class UserLibraryTableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var tableIndexPath: NSIndexPath?

}

extension UserLibraryTableViewCell : UICollectionViewDataSource {

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 12
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("userBooksCollectionViewCell", forIndexPath: indexPath) as! UserLibraryCollectionViewCell



    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ")


}
}

extension UserLibraryTableViewCell : UICollectionViewDelegateFlowLayout {

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let itemsPerRow:CGFloat = 4
    let hardCodedPadding:CGFloat = 5
    let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
    let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
    return CGSize(width: itemWidth, height: itemHeight)
}

}

创建一个协议,然后在
TableViewCell
中创建其实例,并在
UserLibraryViewController
中实现该协议,然后在
CollectionView
didSelectItemAtIndexPath
中使用该委托实例调用如下方法

protocol CustomCollectionDelegate {
    func selectedCollectionCell(indexPath: NSIndexPath)
}

class UserLibraryTableViewCell: UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!
    var tableIndexPath: NSIndexPath?
    var delegate: CustomCollectionDelegate?
}   
现在在
CollectionView
didSelectItemAtIndexPath
中调用此方法

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ")
    self.delegate?.selectedCollectionCell(indexPath)
}
现在,像这样在
UserLibraryViewController
中实现该协议,并在
cellforrowatinexpath
中设置委托

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell
    cell.tableIndexPath = indexPath
    cell.delegate = self
    return cell
} 

extension UserLibraryViewController : CustomCollectionDelegate {
    func selectedCollectionCell(indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("Identifier", sender: indexPath)
    }
}

注意:我添加了带有
nsindepath
作为参数的协议方法,您可以使用自定义对象或
Dictionary/Array

更改它。没有呼叫分机。尝试放置断点。很抱歉,我忘了将委托设置为self,请检查
cellforrowatinexpath
的编辑答案。非常感谢。现在可以按照我的要求工作了。:)您好@NSDoc,我在传递分区中不同数量的项目时遇到问题。我通常知道如何在这里传递不同数量的项目?对于所有部分,相同的项目计数被传递,而不是不同的。请添加带有新代码的新帖子,以及您创建的协议和您获得问题的位置。表右侧有多个部分。那么,您在表格部分面临的问题是什么?