调用放置在数组中的函数

时间:2018-12-06 13:08:22

标签: ios arrays swift swift3 func

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

View Shown when I execute collection view code and getting data from below array

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

1 个答案:

答案 0 :(得分:2)

您的问题是您使用的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来保存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传递给该函数):

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

同样,titleimg变为:

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