Ios 调用放置在数组中的函数

Ios 调用放置在数组中的函数,ios,arrays,swift,swift3,func,Ios,Arrays,Swift,Swift3,Func,我有一个数组,其中按钮的标题作为键,图像作为位置为0的值。我在CollectionView中调用它,该视图看起来像屏幕截图中显示的。在位置1处,我有需要使用didSelectItemAt indexPath调用的函数名。如何在单击时执行该功能 我的收藏视图代码 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

我有一个数组,其中按钮的标题作为键,图像作为位置为0的值。我在CollectionView中调用它,该视图看起来像屏幕截图中显示的。在位置1处,我有需要使用didSelectItemAt indexPath调用的函数名。如何在单击时执行该功能

我的收藏视图代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    let myCell:HomeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell
    myCell.backgroundColor = UIColor.clear
    myCell.layer.borderWidth = 1
    myCell.layer.borderColor = UIColor.clear.cgColor
    myCell.layer.cornerRadius = 10

    let title = "\((mainBtnsArr[indexPath.row] as NSDictionary).allKeys[0])"
    let img = "\(((mainBtnsArr[indexPath.row] as NSDictionary).allValues[0] as! NSArray)[0])"

    myCell.imgIcon.image = UIImage(named: img)
    myCell.imgIcon.contentMode = .center
    myCell.imgIcon.backgroundColor = UIColor.white
    myCell.imgIcon.layer.cornerRadius = 10
    myCell.imgIcon.layer.shadowColor = UIColor.colorFromCode(0xcccccc).cgColor
    myCell.imgIcon.layer.shadowOpacity = 0.5
    myCell.imgIcon.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
    myCell.imgIcon.layer.shadowRadius = 5

    myCell.lbl_Service.text = title
    myCell.lbl_Service.textColor = Constants.sharedInstance.MediumTextColour

    return myCell as HomeCollectionViewCell;
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("User tapped on \(indexPath.row)")
    //let funcToCall: = ((mainBtnsArr[indexPath.row] as NSDictionary).allValues[0] as! NSArray)[1]
    let funcToCall:CGFunction = ((mainBtnsArr[indexPath.row] as NSDictionary).allValues[0] as! NSArray)[1] as! CGFunction

}

// CollectionView Clicks
func ShowHotelSearch() {
    presenter.showHotelSearchScreen()
}
func ShowFlightSearch() {
    MyCustomAlert.sharedInstance.ShowAlert(vc: self, myTitle: StringClass.sharedInstance.lcStr_comingsoon, myMessage: StringClass.sharedInstance.lcStr_comingsoonflightstext)
}

您的问题是,您使用的是一个
字典
,其值是
[Any]
数组,用于保存按钮图像名称和函数。然后,你必须做一些丑陋的演员,以获得一些你可以使用的东西:

if let funcToCall = myBtnsArr[indexPath.row].first?.value.last as? (HomeViewController) -> () -> () {
    funcToCall(self)()
}
最好使用适当的类型将其放入
struct

struct ButtonInfo {
    let title: String
    let image: String
    let function: (HomeViewController) -> () -> ()
}
注意:此处定义的属性
函数
用于保存
HomeViewController
类的实例函数。调用函数时,
HomeViewController
(即
self
)的当前实例将按如下方式传递:
function(self)(

然后像这样定义您的
mainBtnsArr

let mainBtnsArr = [
    ButtonInfo(title: "Hotels", image: "homepage_hotel", function: ShowHotelSearch),
    ButtonInfo(title: "Flights", image: "homepage_flights", function: ShowFlightSearch),
    ButtonInfo(title: "Transfer", image: "homepage_transfer", function: ShowTransferSearch),
    ...
]
这样使用数据就容易多了。(注意:由于数组中的函数是实例函数,因此在调用它们时必须将
self
传递给函数):

同样地,
title
img
成为:

let title = mainBtnsArr[indexPath.row].title
let img = mainBtnsArr[indexPath.row].image

您的问题是,您使用的是一个
字典
,其值是
[Any]
数组,用于保存按钮图像名称和函数。然后,你必须做一些丑陋的演员,以获得一些你可以使用的东西:

if let funcToCall = myBtnsArr[indexPath.row].first?.value.last as? (HomeViewController) -> () -> () {
    funcToCall(self)()
}
最好使用适当的类型将其放入
struct

struct ButtonInfo {
    let title: String
    let image: String
    let function: (HomeViewController) -> () -> ()
}
注意:此处定义的属性
函数
用于保存
HomeViewController
类的实例函数。调用函数时,
HomeViewController
(即
self
)的当前实例将按如下方式传递:
function(self)(

然后像这样定义您的
mainBtnsArr

let mainBtnsArr = [
    ButtonInfo(title: "Hotels", image: "homepage_hotel", function: ShowHotelSearch),
    ButtonInfo(title: "Flights", image: "homepage_flights", function: ShowFlightSearch),
    ButtonInfo(title: "Transfer", image: "homepage_transfer", function: ShowTransferSearch),
    ...
]
这样使用数据就容易多了。(注意:由于数组中的函数是实例函数,因此在调用它们时必须将
self
传递给函数):

同样地,
title
img
成为:

let title = mainBtnsArr[indexPath.row].title
let img = mainBtnsArr[indexPath.row].image


现在发生了什么?肯定有比你现在做的更好的方法。你能分享一些你的方法吗?它们可能都是通用的。当我单击集合视图单元格时。我希望函数被执行。说如果我点击酒店,我想让ShowHotelSearch工作你走错方向了!!!使用标签和基于标签的隔离怎么样?现在发生了什么?肯定有比现在更好的方法。你能分享一些你的方法吗?它们可能都是通用的。当我单击集合视图单元格时。我希望函数被执行。说如果我点击酒店,我想让ShowHotelSearch工作你走错方向了!!!如何使用标记并基于这些标记进行分离?您好,我在为mainBtnsArr编译时出错------->'(标题:字符串,图像:字符串,函数:(HomeViewController)->()->()->())不能转换为(标题:字符串,图像:字符串,函数:@escaping()->())'请使用静态函数ShowHotelSearch(){print(“ShowHotelSearch”)}而不是您以前的method@YogeshTandel,请查看我的编辑。由于您正在调用实例函数,因此需要将类型指定为
(HomeViewController)->()->()
struct
中,然后在调用函数时传递
self
。@vacawama-Awesome bro.Works:)您好,我在为mainBtnsArr编译时出错---->'(标题:字符串,图像:字符串,函数:(HomeViewController)->()->())不能转换为'(标题:String,图像:String,函数:@escaping()->())'请使用静态func ShowHotelSearch(){print(“ShowHotelSearch”)}而不是旧的method@YogeshTandel,请查看我的编辑。由于您正在调用实例函数,因此需要将类型指定为
(HomeViewController)->()->()
struct
中,然后在调用函数时传递
self
。@vacawama-Awesome bro.Works:)