I want to add a uiviewcontroller’s view which has a button and few labels as a content view of a uitableviewcell.
Though i am able to add but the button action causes a crash.
MyViewController *controller = [[MyViewController alloc] initwithnibname:@"MyView" bundle:nil];
[cell.contentview addsubview:controller.view];
[controller release]; // if i comment out this part the button action is received by my view controller.
However there are memory leaks when its removed from view. The dealloc of myviewcontroller is not called.
What is the correct way to do this?
-
Add a view to a uitableview cell
which has a button and is handled by
the viewcontroller -
How to assure memory is released
when the view goes out of scope?
TIA,
Praveen
I think the problem is, that you are releasing the controller and just using the subview which is retained by its superview. The action pattern needs a target which I assume is the released controller. And why should you release your viewController if you only need the view of it? Retain it and keep a reference through a property to it.
My way of adding subviews to a tableview cell would be in a subclass of UITableViewCell. Let’s assume you are having a subclass of
UITableViewCell, sayButtonTableViewCell. The init of the of cell creates and adds a UIButton to your cell and puts it nicely in its contentView. Decalre a property which references to the button. LikeUIButton *myButton. What should be done in the cellForRowAtIndexPath is something like this:I’ve made up the initializer
initWithReuseIdentifierwhich can be easily implemented.You assure release of memory with the viewDidUnload method of the UIViewController and the dealloc method.