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,我在传递分区中不同数量的项目时遇到问题。我通常知道如何在这里传递不同数量的项目?对于所有部分,相同的项目计数被传递,而不是不同的。请添加带有新代码的新帖子,以及您创建的协议和您获得问题的位置。表右侧有多个部分。那么,您在表格部分面临的问题是什么?