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]]
]
My Collection View Code
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)
}
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:You'd be better served to put this into a
struct
using proper types:Note: The property
function
is defined here to hold an instance function of theHomeViewController
class. When calling the function, the current instance ofHomeViewController
(i.e.self
) will be passed like this:function(self)()
.Then define your
mainBtnsArr
like this: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):Likewise,
title
andimg
become: