Reputation: 1581
I have a UItableViewCell defined from a XIB file. This cell contains a UIScrollView, which is filled on the fly with UIView Objects - viewXY -. I use arc, everywhere in the code. The UI looks like:
cell 0: [ view00 view01 view02 ...]
cell 1: [ view10 view11 view12 ...]
etc..., where [ ] is a cell content, which is horizontally scrollable.
Problem: I profiled the app, and as I scroll down vertically, the memory footprint grows rapidly, and as far as I could see, never goes back to normal, nor reaches a plateau. More importantly, if I scroll down a few cells and then back up, memory increases too....
I checked for leaks with the instrument tool, nothing.
I narrowed down the problem around the part of the code that creates the viewXY . If I use:
myDisplay* viewXY;//myDisplay is a UIView subclass
-->viewXY = [[NSBundle mainBundle] loadNibNamed:AW_UI_VE owner:self options:nil][0];
[scrollView addSubview:viewXY];
Memory grows out of control as I flip through the cells. The NIB is a simple UIView+UIImageView+2 Labels.... If I replace say
UIImageView *viewXY = [UIImageView alloc] initWithImage:[blablabla]]
[scrollView addSubview:viewXY];
Everything is fine. The Live Bytes in instruments reaches rapidly a plateau.
As part of the tests, I load the NIB file without exercising any of the methods defined in its custom class, so it comes out with the IBOutlets as defined in the IB (default label text, image etc...).
This stuff is driving me crazy.
Suggestions to understand what I'm doing wrong are most welcome.
Upvotes: 0
Views: 322
Reputation: 5184
[scrollView addSubview:viewXY];
There is your problem. You're adding something to the scroll view, and never remove it.
From the sound of things, you aren't using the UITableView properly. You should have a view controller that impliments methods from UITableViewDelegate and UITableViewDataSource protocols, and use those to fill the table with data. You never add anything to the table directly, you only provide it with cells when it requests them.
Upvotes: 0
Reputation: 1329
In your ViewController's viewDidLoad you should have something like this:
[yourTable registerNib:[UINib nibWithNibName:@"yourUITableViewCellNibName" bundle:nil]
forCellReuseIdentifier:@"reuseIdentifier"];
and in your table datasource method(cellForRowAtIndexPath) you should have the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell* cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
//Set your cell's values.
return cell;
}
Upvotes: 1
Reputation: 620
I’m not sure if this is related to the memory usage, but the docs recommend UINib
for this use case:
Your application should use
UINib
objects whenever it needs to repeatedly instantiate the same nib data.
Another possibility is that there is a retain cycle in myDisplay
. Does it have any strong
properties that should be weak
?
Upvotes: 0