I have a View with few subviews in it and I draw some sublayers on the view as small dots. Problem is while deleting them.
I have an array of points and I know index of the one to delete, Now I want to delete the sublayer which was added based on the index of the point in the array.
sublayers of the view returns an NSArray of layers but it has more number of layers than I added and when I remove a sublayer based on the index, some of the subviews of my View get disappeared.
can anyone tell me a good solution ?
Use tags. Set a tag on each view whose value maps to the array index, then remove the views by tag instead of their index in the subviews array.
I suggest using an offset number, so the tags would be 100, 101, 102 etc instead of 0, 1, 2 because 0 is the default tag for all views so removing a view with tag zero will cause you the same problems you are having already.
Here's the code you'll need:
#define TAG_OFFSET 100
//add the views
for (int i = 0; i < [array count]; i++)
{
UIView *view = //create subview based on array index
view.tag = TAG_OFFSET + i;
[containerView addSubview:view];
}
//remove view at index 5
[[containerView viewWithTag:TAG_OFFSET + 5] removeFromSuperview];