Ios 将ReusableCellWithIdentifier和don';你不用手机吗?
如果我打电话:Ios 将ReusableCellWithIdentifier和don';你不用手机吗?,ios,uitableview,memory-leaks,uikit,Ios,Uitableview,Memory Leaks,Uikit,如果我打电话: -[UITableView dequeueReusableCellWithIdentifier] 然后我选择不在这种方法中重用单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 是否必须引入泄漏?我的猜测是,一旦自动释放池耗尽,它就会被释放。是的,dequeueReusableCellWithIdentifier返回
-[UITableView dequeueReusableCellWithIdentifier]
然后我选择不在这种方法中重用单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
是否必须引入泄漏?我的猜测是,一旦自动释放池耗尽,它就会被释放。是的,
dequeueReusableCellWithIdentifier
返回一个自动释放的对象,并且不会导致内存泄漏
但如果您选择不重用单元格(无论出于何种原因),则无需首先调用dequeueReusableCellWithIdentifier
更新:您可以使用以下代码检查内部重用队列中存储了多少单元格:
NSDictionary *reuseDict = [self.tableView valueForKey:@"reusableTableCells"];
NSArray *reuseArray = [reuseDict objectForKey:CellIdentifier];
NSLog(@"%d", [reuseArray count]);
我使用Master Detail Xcode应用程序对此进行了测试,在该应用程序中,我删除了对
dequeueReusableCellWithIdentifier
的调用。重用队列中的单元数量在横向方向增加到最多17个,纵向增加到最多23个。这正是可见细胞的数量加上1。因此,即使您从不重用单元格,其数量也确实是有限的。是的,dequeueReusableCellWithIdentifier
返回一个自动释放的对象,并且不会导致内存泄漏
但如果您选择不重用单元格(无论出于何种原因),则无需首先调用dequeueReusableCellWithIdentifier
更新:您可以使用以下代码检查内部重用队列中存储了多少单元格:
NSDictionary *reuseDict = [self.tableView valueForKey:@"reusableTableCells"];
NSArray *reuseArray = [reuseDict objectForKey:CellIdentifier];
NSLog(@"%d", [reuseArray count]);
我使用Master Detail Xcode应用程序对此进行了测试,在该应用程序中,我删除了对
dequeueReusableCellWithIdentifier
的调用。重用队列中的单元数量在横向方向增加到最多17个,纵向增加到最多23个。这正是可见细胞的数量加上1。因此,即使您从不重用单元,数量也确实是有限的。不,这不会导致泄漏,但也不一定是内存有效的。如果您希望自动处理所有这些单元重用事务,请考虑使用一个框架,如优秀的TabelVIEW(免费)。 < P>不,这不会造成泄漏,但也不一定是内存效率。如果您希望自动处理所有这些单元重用事务,请考虑使用一个框架,如优秀的TabelVIEW(免费)。 <是的,这引入了一个漏洞。通过子类化UITableViewCell
,从经验上尝试这一点:
static int instances = 0;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(@"Init! instances=%d", instances++);
}
return self;
}
-(void)dealloc {
NSLog(@"Dealloc! instances=%d", --instances);
}
如果不使用从dequeueReusableCellWithIdentifier
返回的单元格,则会出现泄漏
此问题有两种解决方案:
dequeueReusableCellWithIdentifier
的结果,或者nil
作为重用标识符
;这样就不会出现泄漏。这是最好的解决方案,除非您的表有很多行,并且您不想处理重用的混乱是的,这会导致泄漏。通过子类化
UITableViewCell
,从经验上尝试这一点:
static int instances = 0;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(@"Init! instances=%d", instances++);
}
return self;
}
-(void)dealloc {
NSLog(@"Dealloc! instances=%d", --instances);
}
如果不使用从dequeueReusableCellWithIdentifier
返回的单元格,则会出现泄漏
此问题有两种解决方案:
dequeueReusableCellWithIdentifier
的结果,或者nil
作为重用标识符
;这样就不会出现泄漏。这是最好的解决方案,除非您的表有很多行,并且您不想处理重用的混乱如果不返回由
-(UITableViewCell*)表视图中的dequeueReusableCellWithIdentifier
返回的UITableViewCell:(UITableView*)表视图cellForRowAtIndexPath:(NSIndexPath*)indexPath代码>然后它可能会产生泄漏或不泄漏,这取决于您正在执行的操作。当释放表视图时,dequeueReusableCellWithIdentifier
返回的单元格将自动释放,因为表视图保存了对此方法返回的每个单元格的引用
例如,如果您在-(CGFloat)tableView:(UITableView*)tableView heightforrowatinexpath:(nsindepath*)indepath中重复调用dequeueReusableCellWithIdentifier
,就会出现问题,如下所示:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
// calculate height
// ...
}
在上面的代码中,dequeueReusableCellWithIdentifier
每次调用此方法时都将返回新的单元格,因此当您滚动表视图时,内存使用量将增加。但是,在稍后的某个时间点,当重新释放表视图时,dequeueReusableCellWithIdentifier
分配的这些单元格将自动释放。如果您不在-(UITableViewCell*)表视图中返回由dequeueReusableCellWithIdentifier
返回的UITableViewCell:然后它可能会产生泄漏或不泄漏,这取决于您正在执行的操作。当释放表视图时,dequeueReusableCellWithIdentifier
返回的单元格将自动释放,因为表视图保存了对此方法返回的每个单元格的引用
例如,如果您在-(CGFloat)tableView:(UITableView*)tableView heightforrowatinexpath:(nsindepath*)indepath中重复调用dequeueReusableCellWithIdentifier
,就会出现问题,如下所示:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
// calculate height
// ...
}
在上面的代码中,dequeueReusableCellWithIdentifier
每次调用此方法时都将返回新的单元格,因此当您滚动表视图时,内存使用量将增加。但是,在稍后的某个时间点,当重新释放表视图时,dequeueReusableCellWithIdentifier
分配的这些单元格将自动释放。我认为如果我不调用dequeue,那么这些单元格将保持排队状态,这是一个漏洞。。。嗯…@Yar:好的,是的,表视图将存储一些单元格以供重用,但我认为数量有限。@Yar:我写这个答案几乎是2年前的事了:)我确信在写它时它是正确的,但从那时起我就没有考虑过。Ye