I'm using Instruments to monitor memory usage. I notice that memory always increase and not release anymore on my UITableViewController. I don't understand why. Following I list my codes
我用仪器来监控内存的使用。我注意到在我的UITableViewController上内存总是增加而不再释放。我不明白为什么。下面我列出我的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int row = [indexPath section];
static NSString *CellIdentifier = @"ApplicationCell";
BookCell *cell = (BookCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil];
cell = bookCell;
self.bookCell=nil;
}
Book *book = [books objectAtIndex:row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.bookNameLabel.text = book.author;
cell.bookPriceLabel.text = book.price;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
3 个解决方案
#1
0
Hie...
快走…
Just try it this way, i hope i would work
就这样试试,我希望我能工作
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { int row = [indexPath section];
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {int row = [indexPath section];
if(cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil]; cell = bookCell; self.bookCell=nil; }
Book *book = [books objectAtIndex:row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.bookNameLabel.text = book.author;
cell.bookPriceLabel.text = book.price; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell; }如果(cell == nil) {[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil];细胞= bookCell;self.bookCell =零;图书= [books objectAtIndex:row];细胞。selectionStyle = UITableViewCellSelectionStyleNone;cell.bookNameLabel。文本= book.author;cell.bookPriceLabel。文本= book.price;cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;返回单元;}
#2
0
Answer #1:
回答1:
If you are referring to memory allocation and not leaking memory, here is a suggestion.
如果您指的是内存分配而不是泄漏内存,这里有一个建议。
Implement viewDidUnload to release parts of your viewcontroller that are no longer needed. Typically, you would re-instantate these objects in viewDidLoad (or loadView if you are not using xib files).
实现viewDidUnload来释放视图控制器中不再需要的部分。通常,您将在viewDidLoad中重新实例化这些对象(或者如果不使用xib文件,则使用loadView)。
You can visualize this by placing NSLog statements inside viewDidUnload and in iphone simulator and click "Hardware->Simulate Memory Warning", unless they already unloaded, most of your non-visible viewcontrollers will run the viewdidunload method.
您可以通过在viewDidUnload和iphone模拟器中放置NSLog语句并单击“硬件->模拟内存警告”来实现可视化,除非它们已经卸载,否则大多数不可见的viewcontroller会运行viewDidUnload方法。
Answer #2:
答案2:
If you are referring to memory leaks, then I would suspect the way you are loading table cells is leaking memory. Here is a good reference for loading tablecells from xibs. Your method might be correct, I'm just not familiar with it.
如果您指的是内存泄漏,那么我将怀疑您加载表单元格的方式是内存泄漏。这里有一个从xibs装载台布的很好的参考。你的方法可能是对的,只是我不熟悉而已。
#3
0
Your code looks leak-free to me.
我觉得你的代码没有漏洞。
I also noticed that the following line looks like it should be causing release of your custom cell object early (assuming the bookCell property is declared as "retain"):
我还注意到,以下一行看起来应该会使您的自定义单元格对象提前释放(假设bookCell属性被声明为“retain”):
self.bookCell=nil;
I would recommend removing it completely. You could also change it to (assuming the instance variable name matches the property name):
我建议把它完全移除。还可以将其更改为(假设实例变量名与属性名匹配):
bookCell=nil;
#1
0
Hie...
快走…
Just try it this way, i hope i would work
就这样试试,我希望我能工作
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { int row = [indexPath section];
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {int row = [indexPath section];
if(cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil]; cell = bookCell; self.bookCell=nil; }
Book *book = [books objectAtIndex:row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.bookNameLabel.text = book.author;
cell.bookPriceLabel.text = book.price; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell; }如果(cell == nil) {[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil];细胞= bookCell;self.bookCell =零;图书= [books objectAtIndex:row];细胞。selectionStyle = UITableViewCellSelectionStyleNone;cell.bookNameLabel。文本= book.author;cell.bookPriceLabel。文本= book.price;cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;返回单元;}
#2
0
Answer #1:
回答1:
If you are referring to memory allocation and not leaking memory, here is a suggestion.
如果您指的是内存分配而不是泄漏内存,这里有一个建议。
Implement viewDidUnload to release parts of your viewcontroller that are no longer needed. Typically, you would re-instantate these objects in viewDidLoad (or loadView if you are not using xib files).
实现viewDidUnload来释放视图控制器中不再需要的部分。通常,您将在viewDidLoad中重新实例化这些对象(或者如果不使用xib文件,则使用loadView)。
You can visualize this by placing NSLog statements inside viewDidUnload and in iphone simulator and click "Hardware->Simulate Memory Warning", unless they already unloaded, most of your non-visible viewcontrollers will run the viewdidunload method.
您可以通过在viewDidUnload和iphone模拟器中放置NSLog语句并单击“硬件->模拟内存警告”来实现可视化,除非它们已经卸载,否则大多数不可见的viewcontroller会运行viewDidUnload方法。
Answer #2:
答案2:
If you are referring to memory leaks, then I would suspect the way you are loading table cells is leaking memory. Here is a good reference for loading tablecells from xibs. Your method might be correct, I'm just not familiar with it.
如果您指的是内存泄漏,那么我将怀疑您加载表单元格的方式是内存泄漏。这里有一个从xibs装载台布的很好的参考。你的方法可能是对的,只是我不熟悉而已。
#3
0
Your code looks leak-free to me.
我觉得你的代码没有漏洞。
I also noticed that the following line looks like it should be causing release of your custom cell object early (assuming the bookCell property is declared as "retain"):
我还注意到,以下一行看起来应该会使您的自定义单元格对象提前释放(假设bookCell属性被声明为“retain”):
self.bookCell=nil;
I would recommend removing it completely. You could also change it to (assuming the instance variable name matches the property name):
我建议把它完全移除。还可以将其更改为(假设实例变量名与属性名匹配):
bookCell=nil;