[英]navigate on click of collectionview cell inside tableview
I have a tableview cell inside which i have added collectionview cell ( for horizontal scrolling). 我有一个tableview单元格,其中添加了collectionview单元格(用于水平滚动)。
Now i want to push to other navigation controller on pressing any cell of horizontal collectionview. 现在我想在按下水平collectionview的任何单元格时推送到其他导航控制器。 How to do it ?
怎么做 ? Or how can i define delegate methods for cell press.
或如何定义单元格印刷的委托方法。
Code : 代码:
ViewController.swift : ViewController.swift:
class ViewController: UIViewController {
var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]
}
extension ViewController : UITableViewDelegate { }
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
return cell
}
}
CategoryRow.swift CategoryRow.swift
class CategoryRow : UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
}
extension CategoryRow : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
return cell
}
}
extension CategoryRow : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> 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)
}
}
VideoCell.swift VideoCell.swift
class VideoCell : UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
}
Here you will get the cell click at the delegate method didSelectItemAtIndexPath
on CategoryRow
class and from there you can fire a delegate to get call inside ViewController
ViewController.swift : 在这里,您将在
CategoryRow
类的委托方法didSelectItemAtIndexPath
处单击单元格,然后可以从该位置激发一个委托以在ViewController
ViewController.swift内部进行调用:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
cell.delegate = self
return cell
}
VideoCell.swift : VideoCell.swift :
protocol CategoryRowDelegate:class {
func cellTapped()
}
CategoryRow.swift : CategoryRow.swift :
class CategoryRow : UITableViewCell {
weak var delegate:CategoryRowDelegate?
@IBOutlet weak var collectionView: UICollectionView!
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if delegate!= nil {
delegate?.cellTapped()
}
}
Add the delegate function inside ViewController
在
ViewController
添加委托函数
func cellTapped(){
//code for navigation
}
I think its better to use Notification in this case. 我认为在这种情况下最好使用Notification。
post a notification in didSelectItem
of collection view 在集合视图的
didSelectItem
中发布通知
NSNotificationCenter.defaultCenter().postNotificationName(notificationIdentifier, object: nil)
and add an observer in viewController viewDidLoad
as follows 并在viewController
viewDidLoad
添加一个观察者,如下所示
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(pushToNew(_:)), name: notificationIdentifier, object: nil)
in the new pushToNew
function, perform your segue 在新的
pushToNew
函数中,执行搜索
func pushToNew(notification: Notification) {
// perform your segue here. Navigate to next view controller
}
First create protocol for delegation from CategoryRow.swift like below code 首先从CategoryRow.swift创建委派协议,如下代码
protocol CollectionViewSelectionDelegate: class {
func didSelectedCollectionViewItem(selectedObject: AnyObject)
}
Now create delegate object on VideoCell.swift like below 现在在VideoCell.swift上创建委托对象,如下所示
weak var delegate:CollectionViewSelectionDelegate?
Change ViewController.swift code before return cell 在返回单元格之前更改ViewController.swift代码
cell?.delegate = self
Override method of delegate in ViewController.swift and call similar method from VideoCell.swift from UICollectionView Delegate method. 重写ViewController.swift中的委托方法,并从UICollectionView Delegate方法中调用VideoCell.swift中的类似方法。
Make a protocol 制定协议
protocol collectionViewCellClicked{
func cellClicked()
}
Implement this protocol in main View Controller Your View Controller look like this 在主View Controller中实现此协议。View Controller如下所示
class ViewController: UITableViewController, collectionViewCellClicked{ func cellClicked(){ // Your Code }}
Your cellForRowAt delegate look like this 您的cellForRowAt委托看起来像这样
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: <#T##String#>, for: <#T##IndexPath#>) cell.delegate = self return cell }
In your Table View Cell Make a variable of type collectionViewCellClicked var delegate: collectionViewCellClicked?
在表视图单元格中,创建一个类型为collectionViewCellClicked的变量变量
var delegate: collectionViewCellClicked?
and in your didSelectItemAt delegate 并在您的didSelectItemAt委托中
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { delegate.cellClicked() }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.