简体   繁体   English

将目标添加到tableview单元格内的collectionview单元格的按钮

[英]Add target to button of collectionview cell inside tableview cell

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 button of any cell of horizontal collectionview. 现在,我想在水平collectionview的任何单元格的按钮上按下其他导航控制器。 How to do it ? 怎么做 ? O Ø

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

cell.button.addTarget(self, action:#selector(ViewController.goToLookAtPage(_:)), for: .touchUpInside)
        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)
    }

}

where do i declare goToLookAtPage function ? 我在哪里声明goToLookAtPage函数?

VideoCell.swift VideoCell.swift

class VideoCell : UICollectionViewCell {

    @IBOutlet weak var button: UIButton!
}

You need to delclare it on CategoryRow Class also declare global closure 您需要在CategoryRow类上进行声明,还需要声明全局关闭

like 喜欢

var buttonTapped:((CategoryRow?) -> Void)? = nil

Now we have closure to call 现在我们可以closure

Implement goToLookAtPage as below 如下实现goToLookAtPage

func goToLookAtPage(_ sender:UIButton) {

    if let btnAction = self.buttonTapped {
         btnAction(self)
      }
}

Now in your ViewController add following in cellForRowAtIndexPath 现在在您的ViewControllercellForRowAtIndexPath添加以下内容

cell.buttonTapped = {(cell) -> Void in
          //You Got your response , do push in main Thread  
}

Hope it helps to you 希望对您有帮助

In your button handler function get the button's position where sender is your button. 在按钮处理程序函数中,获取按钮的位置,其中发送者是您的按钮。

let position = sender.convert(CGPoint.zero, to: self.collectionView)

Get the index path for the given position. 获取给定位置的索引路径。

let indexPath = self.collectionView?.indexPathForItem(at: position)

Get the model from your data source with the index path. 使用索引路径从数据源中获取模型。

let model = self.data[indexPath.row]

Now that you have your data model, pass it to your destination and push your view controller or whatever. 现在您已经有了数据模型,将其传递到目的地并推送视图控制器或其他任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM