#selector具有本地功能

时间:2017-07-16 21:30:18

标签: swift

我正在构建这个应用程序,以显示我家乡周围的餐馆,并给他们一个菜单,开放时间和地址。我在这里有这个功能,在集合视图中显示有关餐厅的信息

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! RestaurantCollectionViewCell

    cell.addressLabel.text = addressArray[indexPath.row]
    cell.restaurantNameLabel.text = restaurantArray[indexPath.row]
    cell.openingTimeLabel.text = openingTimeArray[indexPath.row]
    cell.chosenRestaurantButton.addTarget(self, action: #selector(getProducts), for: .touchUpInside)

    return cell
}

这是我传递给集合视图函数中#selector的函数。问题是我需要getProducts函数中集合视图函数的索引路径。我无法嵌套它抛出有关本地函数的错误的函数。我可以在getProducts函数中获取索引路径吗?

func getProducts() {
    let viewController = storyboard?.instantiateViewController(withIdentifier: "productsCollectionView") as! ProductsViewController
    viewController.restaurantChosen = restaurantArray[indexPathVariable.row]
    self.present(viewController, animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:4)

使用可重复使用的单元格中的按钮,您无法执行此类操作。为什么不在CountryCollectionViewCell子类中使用委托?

protocol RestaurantCollectionViewCellDelegate: class
{
    func getProducts(indexPath: IndexPath)
}

class RestaurantCollectionViewCell: UICollectionViewCell
{
    var indexPath: IndexPath!
    weak var delegate: RestaurantCollectionViewCellDelegate?

    @IBAction func buttonPressed(_ sender: UIButton)
    {
        self.delegate?.getProducts(indexPath: self.indexPath)
    }
}

在CollectionView控制器中,只需像这样分配代理:

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, RestaurantCollectionViewCellDelegate
{
    func getProducts(indexPath: IndexPath)
    {
        let viewController = storyboard?.instantiateViewController(withIdentifier: "productsCollectionView") as! ProductsViewController
        viewController.restaurantChosen = restaurantArray[indexPath.row]
        self.present(viewController, animated: true, completion: nil)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCustomCVCell", for: indexPath) as! RestaurantCollectionViewCell
        cell.indexPath = indexPath
        cell.delegate = self
        return cell
    }
}

答案 1 :(得分:1)

您无需将indexPath传递给按钮处理程序。您可以从按钮本身确定indexPath。

首先,更新按钮处理程序以包含按钮作为参数:

func getProducts(_ sender: UIButton) {
}

然后,您可以从集合视图中按钮的位置计算indexPath。

let position = sender.convert(CGPoint.zero, to: collectionView)
let indexPath = collectionView.indexPathForItem(at: position)

以下是您的完整方法:

func getProducts(_ sender: UIButton) {
    let position = sender.convert(CGPoint.zero, to: collectionView)
    if let indexPath = collectionView.indexPathForItem(at: position) {
        let viewController = storyboard?.instantiateViewController(withIdentifier: "productsCollectionView") as! ProductsViewController
        viewController.restaurantChosen = restaurantArray[indexPath.row]
        self.present(viewController, animated: true, completion: nil)
    }
}

此代码假定cell.chosenRestaurantButtonUIButton。它还假设您的视图控制器具有collectionView属性。