调用放置在数组中的函数

Calling a function which is placed in an Array

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

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]]
]

我的Collection查看代码

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] 数组来保存按钮图像名称和功能。然后你必须做一些丑陋的铸造才能得到你可以使用的东西:

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

注意:这里定义了属性function来保存HomeViewControllerclass的一个实例函数。调用该函数时,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):

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

同样,titleimg 变为:

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