RK-
RK-

Reputation: 12211

managing subViews in iOS

I am creating a Custom Grid kind of View for my App. I am placing many subviews on them. I have a necessity to reload the Gird with new set of Views based on users' interaction and when new data arrives.

I allocate memory for my subviews as this:

while(index < count)

CustomGridTile *view = [[CustomGridTile alloc] initWithFrame:frame];

[self addSubView:view];

[view release];

}

When I wanted to refresh my Grid, I remove all subviews from their superViews and create new set of Grid Tiles (subviews), add to the Custom Gird View and release them.

Am I doing things correctly? Can this bring me memory related issues?

Upvotes: 1

Views: 346

Answers (2)

Khurram Ali
Khurram Ali

Reputation: 835

As long as you are removing the subview it should release memory.

Explanation:

When you allocate the view CustomGridTile its retain count becomes 1. When you add it as the sub view, its retain count would become 2. When you release, retain count becomes 1. Finally when you remove the subview, its retain count will become zero and should be released.

Upvotes: 2

DanZimm
DanZimm

Reputation: 2568

releasing the views can bring some issues - i suggest attempting an autorelease on them or something along those lines

otherwise what i would do is when you remove it from the superview release it as well, but releasing after you add it as a subview has given me troubles in the past

Upvotes: 0

Related Questions