I am trying to add a button to a cell in my cellForRowAtIndexPath method. That works but I seem to be adding a button every time the cell is updated (discovered this because I have updated the wrong screen position and I have 2 buttons that both work and can be clicked).
When is the correct time to allocate and release a UIButton thats added to a cell? Surely I cannot release the button at the end of cellForRowAtIndexPath – that would destroy the button object wouldn’t it? All of the snippets I’ve found with buttons added to cells so far seem to keep instantiation new UIButton objects within cellForRowAtIndexPath which has me somewhat confused..
A couple of things:
Buttons are appearing elsewhere because you’re using the same cell identifier for different types of cells. Each type of cell should get it’s own identifier.
In terms of the button disappearing, I don’t know why that’s happening as a result of
reloadRowsAtIndexPaths, but if you move the button to the front of the other subviews, you won’t lose it.You probably want to
autoreleaseyour UITextField. (Same true with NSString intitleForHeaderInSection.)Anyway, the
cellForRowAtIndexPathbecomes something like:Personally, I might split this into separate methods, but that’s up to you.