Hello,
I have a scrollerView where I am programatically adding UIButtons depending on the data I receive. I have one UIButton added to the scrollerView per data entry.
Because the scrollerView doesn't handle touchEvents as a normal UIView, I subclassed UIButton delegate to handle the the touch events on the UIButtons formed within the scrollerView. Everytime I programitcally added a UIButton, I would append it to my array of UIButtons.
var allButtonsArray: [UIButton] = []When the UIButton delegate touch event gets called, I want to iterate through all the buttons in the array to find out which button the user touched in the scrollerView.
I'm using this. I'm trying to find the index of the button clicked from the allButtonsArray.
for touch: AnyObject in touches{
var location = touch.locationInView(self.theScrollerView)
for buttonView in allButtonsArray{
if(CGRectContainsPoint(buttonView.frame, location)){
let foundIndex = allButtonsArray.indexOf(buttonView){// <- Error compiler error here
}
}
}
}
}The error I get is:
Cannot invoke 'indexOf' with an argument of list type (UIButton)
What do I do?
In order to use
indexOf,your code should adopt the Equatable protocol. By adopting this protocol the list can then compare item with other items to find the desired index.