In my app, I use custom UITableViewCells loaded from a XIB. Each cell contains 4 objects, like a UIImageView, UILabel, UITextField, etc…
I set the tag of each object in my cellforrowatindexpath so each object has the same tag which also is the row of the cell in the tableview.
The problem is, how am I suppose to access these objects in a cell if they all have the same tag? How would I properly access these objects in a method that doesn’t have a NSIndexPath parameter?
Thanks!
I personally try avoiding the use of tags as I usually overuse them to the point of utter confusion and have begun finding more elegant ways around it. For instance, in your situation I would create
IBOutlets for the customUITableViewCell‘s objects making them more semantically relevant. However, your situation might be different – there is not enough in the question for me to say this would definitely work for you.That said, it sounds like you could accomplish what you’re trying to do by:
cellForRowAtIndexPathmethod a tag (before returning it)That way you can access the cell’s objects (presumably
UIViews) from (anywhere) in yourUITableViewControllerby:Keep in mind that this would allow you to access an object in a specific cell. If you were trying to access the objects of all your cells, you could simply iterate through all of them. However, I sense that this would start becoming unnecessarily complicated at this point. It would help if you posted up a bit more information on what exactly you were trying to achieve by utilising the tagging this way. Hope this helps in the meantime.