Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"A trick often used by iOS devs is to use the tag of a view to hold lookup information, such as offset in arrays. With Android you can shove the entire object into the tag; pretty useful."

Yes, but if it's something like a ListView, don't put offset in array there, because the views are recycled (when user scrols), so you're likely to end up with corrupted data. I got bitten by this in the beginning. You can go to great lengths to prevent this from happening, but it will be hard for a reason, because that's abusing the design principles.

I'd recommend reading up on the ViewHolder pattern in this context.

Long story short: cache all the references to all specific sub-views of the row view in the tag (so you don't have to retrieve them again and again) - that's good practice. Just don't store any position-specific data in it.

Watch http://youtu.be/wDBM6wVEO70?t=9m50s - very informative



You can also just create a category on UIView that defines a property which could store a object.

@property (nonatomic, strong) id context;

or

@property (nonatomic, weak) id context;

if you are sure the context will always exist.


If you use ViewHolder, consider using custom views instead:

http://blog.xebia.com/2013/07/22/viewholder-considered-harmf...

I refactored all my code after reading this, and now I consider the ViewHolder an antipattern.

It is incredible how much cleaner and readable my codebase is with custom views. And the performance is the same.


Often used?

Number of times I have used view tags: Once.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: