我有点难以理解出了什么问题以及如何解决这个问题。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:@"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
这不能正常工作,但可以:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:@"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
它们都由“delegate.destinationArray1”填充,但是当所有代码都在花括号内时
if(cell == nil)
列表变得无序并重复单元格并遗漏一些。我不能使用后一种方式,因为它会在滚动时造成大量内存泄漏。
有什么想法吗?
最佳答案
当我开始使用 UITableViews 时,我做了完全相同的事情。内存泄漏的原因是,在第二个实现(有效的那个)中,您实际上每次都在创建每个单元格。让我试着解释一下。
您永远不想在 (cell == nil)
之间设置单元格的内容。这样做的原因是 reuseIdentifier
。如果表格需要显示一个新的单元格,它会抓取一个并查看它是否已经被分配/初始化。如果它有它只会使用它。如果是这种情况,内容将已经设置在您抓取的单元格中,并且您没有告诉它进行任何不同的设置。
(cell == nil)
之间只创建和建立 View 。不是内容。所有内容都要在后面设置。因此,无论它捕获哪个单元格,它始终可以设置内容。这就是您想要的:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) // or (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.font = [UIFont fontWithName:@"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
where.tag = 1;
[cell addSubview:where];
}
NSUInteger row = indexPath.row;
UILabel *where = [cell viewWithTag:1];
where.text = [delegate.destinationArray1 objectAtIndex:row];
return cell;
}
我刚刚在 StackoverFlow 中编写了这个代码,如果有任何小的语法错误,我们深表歉意。
关于iOS tableView 单元格缓存问题未加载每个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14637394/