Iphone UITableView中单元格FromRowAtIndexPath的内存增加
我有一个UITableView,它有一个Iphone UITableView中单元格FromRowAtIndexPath的内存增加,iphone,cocoa-touch,uikit,uitableview,Iphone,Cocoa Touch,Uikit,Uitableview,我有一个UITableView,它有一个cellForRowAtIndexPath方法,可以创建UITextView,然后将其添加到单元格中。未检测到内存泄漏,但在运行工具(对象分配)时,网络内存单向达到18MB,因此崩溃 我的应用程序不断地在tableView数据源中添加和删除单元格,但由于TextView被释放,我看不出内存会堆积起来 这是我的cellforrowatinexpath代码: static NSString *cellIdentifier=@"Cell"; UITableVie
cellForRowAtIndexPath
方法,可以创建UITextView,然后将其添加到单元格中。未检测到内存泄漏,但在运行工具(对象分配)时,网络内存单向达到18MB,因此崩溃
我的应用程序不断地在tableView数据源中添加和删除单元格,但由于TextView被释放,我看不出内存会堆积起来
这是我的cellforrowatinexpath
代码:
static NSString *cellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
// Build cell
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UITextView *tv=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 95)];
tv.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
tv.text=@"My Cell Text";
[cell addSubview:tv];
[tv release];
return cell;
提前谢谢 每次提取单元格时都会添加UITextView。如果它来自缓存,它已经有一个作为子视图,所以现在它有两个,然后是三个,然后是四个 因此,只需将它们添加到新单元格中:
谢谢,这很好用,除非数据源像我的情况一样被更改了怎么办?单元格仍然显示缓存的数据?在本例中,它将显示您将文本设置为“hi”的任何内容。。。如果需要不同的文本,可以在此处进行设置-具体取决于indexPath。如果需要在显示时对其进行更改,则需要重新加载给定的indexath,即[self.tableView reloadrowsatindexpath:…with animation:…];您可以使用Instruments的Leaks模板来调试这类事情。泄漏仪器将显示仍然存在但未知的物体;ObjectAlloc工具将显示所有对象,因此您可以按类和实例深入查看它们可能包含的内容。
if (cell==nil) {
// Build cell
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UITextView *tv=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 95)];
tv.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
tv.tag = 42; // Or whatever, if you don't want to use custom cells
[cell addSubview:tv];
[tv release];
}
UITextView *textView = (UITextView *) [cell viewWithTag:42];
textView.text=@"hi";