调用放置在数组中的函数 [英] Calling a function which is placed in an Array

查看:128
本文介绍了调用放置在数组中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其中按钮的标题为键,图像的值为位置0.我在CollectionView中调用此视图,该视图类似于屏幕快照中的show.在位置1,我具有需要使用didSelectItemAt indexPath调用的函数的名称.如何在点击时执行功能?

I have an array where I have the Title of Button as Key and the Image as Value with position 0. I am calling this in CollectionView and the view looks like show in screenshot. At position 1 i have the name of the function that needs to be called with didSelectItemAt indexPath. How do I execute the function on click?

var mainBtnsArr = [
    ["Hotels" : ["homepage_hotel", ShowHotelSearch]],
    ["Flights" : ["homepage_flights", ShowFlightSearch]],
    ["Transfer" : ["homepage_transfer", ShowTransferSearch]],
    ["Activities" : ["homepage_sightseeing", ShowSightSeeingSearch]],
    ["Cruises" : ["homepage_cruise", ShowCruiseSearch]],
    ["CarRental" : ["homepage_carrental", ShowCarrentalSearch]],
    ["Packages" : ["homepage_packages", ShowPackageSearch]],
    ["Visa" : ["homepage_visa", ShowVisaSearch]],
    ["Groups" : ["homepage_groups", ShowGroupSearch]],
    ["Insurance" : ["homepage_insurance", ShowInsuranceSearch]],
    ["Meals" : ["homepage_meals", ShowMealsSearch]],
    ["ArrivalGuides" : ["homepage_arrivalguide", ShowArrivalGuidesSearch]]
]

我的收藏集查看代码

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)
}

推荐答案

您的问题是您使用的Dictionary的值是[Any]数组,用于保存按钮图像的名称和功能.然后,您必须进行一些丑陋的转换才能获得可以使用的东西:

Your problem is that you are using a Dictionary with a value which is an [Any] array to hold the button image name and function. Then you have to do some ugly casting to get to something you can use:

if let funcToCall = myBtnsArr[indexPath.row].first?.value.last as? (HomeViewController) -> () -> () {
    funcToCall(self)()
}

最好使用适当的类型将其放入struct:

You'd be better served to put this into a struct using proper types:

struct ButtonInfo {
    let title: String
    let image: String
    let function: (HomeViewController) -> () -> ()
}

注意:此处定义属性function来保存HomeViewController类的实例函数.调用该函数时,将像这样传递HomeViewController(即self)的当前实例:function(self)().

Note: The property function is defined here to hold an instance function of the HomeViewController class. When calling the function, the current instance of HomeViewController (i.e. self) will be passed like this: 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传递给该函数):

Then it is much easier to use the data. (Note: Since the functions in the array are instance functions, you must pass self to the function when calling them):

let funcToCall = mainBtnsArr[indexPath.row].function
funcToCall(self)()

同样,titleimg变为:

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

这篇关于调用放置在数组中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆