viewForHeaderInSection创建内存泄漏? [英] viewForHeaderInSection creating a memory leak?

查看:93
本文介绍了viewForHeaderInSection创建内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用自定义标题视图和委托方法tableView:viewForHeaderInSection:获得我想要的外观,我取得了巨大的成功。但是我认为它正在产生内存泄漏,我不确定该怎么办。

I am having great success getting the look I want using a custom header view and the delegate method tableView: viewForHeaderInSection:. But I think it is producing a memory leak, and I'm not sure what to do about it.

代码是这样的:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSLog (@"New header, section %d", section);
    ResultsHeaderView *header = [[ResultsHeaderView alloc] initWithFrame:CGRectMake(0, 0, defaultResultsHeaderSize.width, defaultResultsHeaderSize.height)];

    SearchResult *result = [[[[self.fetchedResultsController sections] objectAtIndex:section] objects] objectAtIndex:0];

    header.text = result.searchUsed.keywords;
    header.searchTermsEntity = result.searchUsed;
    header.resultDelegate = self;
    header.section = section;

    return [header autorelease];
}

如您所见,每次调用它都会实例化一个新对象

As you can see, every time this is called, it instantiates a new object of type ResultsHeaderView, which is a subclass of UIView.

问题是每次每次将节标题从视图中滚动出然后再次打开时,它都经常被调用,它被调用。添加新节后,即使是其他节,它也会多次调用(尽管我可能对此有一些控制权,我将对其进行研究。)

The problem is that it is called often, every time a section header is scrolled off of the view and then back on, it gets called. It gets called multiple times when a new section is added, even for the other sections (although I may have some control over that, and I'm going to look into it.)

我想知道是否有诸如tableView:dequeueReusableCellWithIdentifier:之类的东西可以管理节标题视图,或者知道何时需要节标题视图的方法。我不确定自动释放是否足以避免泄漏。

I am wondering if there is something like tableView:dequeueReusableCellWithIdentifier: that can manage section header views, or a way to know when a section header view is in need of a release. I am not sure if the autorelease is sufficient to avoid a leak.

同时,我的理解是创建单元格成本很高,这就是为什么它们可以重复使用的原因dequeueReusableCellWithIdentifier进程。我必须想象这与节标题相同。

At the same time, my understanding is that creating cells is costly, and that's why they get reused with the dequeueReusableCellWithIdentifier process. I have to imagine this would be the same with section headers.

会在发表评论之前遇到这个问题的人吗?

Would anyone who has come across this issue before comment?

推荐答案

这是我决定要做的,除非有人看到其中的缺陷或提出一个更好的主意。

Here is what I have decided to do, unless someone can see a flaw in it or can come up with a better idea.

在管理tableView的视图控制器中,我添加了一个属性

In the view controller that manages the tableView, I added a property

NSMutableArray *viewsForSectionHeaders;

然后我修改了tableView:viewForHeaderInSection:

Then I have modified my tableView:viewForHeaderInSection:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    SearchResult *result = [[[[self.fetchedResultsController sections] objectAtIndex:section] objects] objectAtIndex:0];

    ResultsHeaderView *header;

    if (section < [viewsForSectionHeaders count]) {
        header = [viewsForSectionHeaders objectAtIndex:section];
    }
    else {
        header = [[[ResultsHeaderView alloc] initWithFrame:CGRectMake(0, 0, defaultResultsHeaderSize.width, defaultResultsHeaderSize.height)] autorelease];
        [viewsForSectionHeaders addObject:header];
    }

    header.text = result.searchUsed.keywords;
    header.searchTermsEntity = result.searchUsed;
    header.resultDelegate = self;
    header.section = section;

    return header;
}

在这种情况下,我有一系列的标题视图。这并不是立即显而易见的,但是无论何时更新fetchedResultsController,它都可能会将先前关联的标头与原始匹配的标头匹配到不同的部分。但这并不重要,因为标题属性(文本,s​​earchTermsEntity,resultDelegate和section)都在上面的代码中重置了。

In this case, I have an array of header views. It's not immediately obvious, but whenever the fetchedResultsController is updated, it may "mismatch" the previously associated headers with different sections than they were originally matched to. But this doesn't really matter because the header properties (text, searchTermsEntity, resultDelegate, and section) are all reset in the code above.

所以,直到我看到一些东西我想念了,这似乎只是为了在需要时实例化头视图,并重用已经创建的视图。

So, until I see something I missed, this looks like it serves the purpose of only instantiating header views when they are needed, and reusing those that have already been created.

(请注意,我已经移动了在分配视图的位置内部有一个autorelease。我在引用一个nil对象时遇到了一个错误,并对此进行了修复。此外,现在我想到了,在return语句中使用autorelease将对一个对象重复该autorelease如果它是在上一次调用此代码时实例化的,则它会多次出现。我不知道这是否是一个真正的问题,但是很容易理清这个问题,而我当时还是这样做的。)

(Note that I moved the autorelease inside where the view is allocated. I had an error where I was referring to a nil object, and this fixed it. Also, now that I think of it, havint the autorelease in the return statement would have repeated the autorelease on an object multiple times if it had been instantiated in a previous call to this code. I don't know if that is a real problem, but it was easy to straighten that out, while I was at it.)

现在,我不删除节,因此不必担心释放标题视图。但是,如果我确实删除了部分,则意味着标题视图也应从viewsForSectionHeaders数组中删除。

Right now, I don't delete sections, so I don't have to worry about releasing header views. But if I did delete sections, then that means that a header view should be removed from the viewsForSectionHeaders array, too.

这篇关于viewForHeaderInSection创建内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆