UITableview应该只加载第一个可见的单元格?我的tableview最初正在加载每个单元格,这会大大减慢它的速度。我用了大约1000行。只希望它在必要时加载单元格(如用户向下滚动)。任何人都有任何想法为什么这样做?
答案 0 :(得分:1)
我知道最初会为每个单元格调用cellForRowAtIndexPath。细胞的高度是89.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UILabel *textName = nil;
UIImageView* image = nil;
unsigned int DATA_TAG = 1001;
unsigned int IMG_TAG = 1002;
// Retrieve a cell is Available
cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Check if no new cell was available
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// Set the Accessory Data
textName = [[[UILabel alloc]initWithFrame:CGRectMake(cell.frame.origin.x, 80, cell.frame.size.width, 20)]autorelease];
textName.tag = DATA_TAG;
textName.textAlignment = UITextAlignmentCenter;
textName.backgroundColor = [UIColor clearColor];
textName.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
textName.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
textName.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:textName];
//Set the Image Data
image = [[[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 80)]autorelease];
image.tag = IMG_TAG;
image.contentMode= UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:image];
}
Accessory* acc= [[AccessoryManager sharedManager].currentList objectAtIndex:indexPath.row];
if(acc == nil)
return cell;
textName= (UILabel*)[cell.contentView viewWithTag:DATA_TAG];
textName.text= acc.accessoryName;
image= (UIImageView*)[cell.contentView viewWithTag:IMG_TAG];
[image setImage:acc.accessoryImage];
return cell;
}
答案 1 :(得分:0)
请你发一些代码吗?方法-tableView:cellForRowAtIndexPath仅在潜在单元的新“槽”打开时被调用,因此您需要做一些主要错误的事情才能最初“加载每个单元格”!
您是否意味着您最初正在加载所有数据,并希望批量执行此操作?
答案 2 :(得分:0)
这是对的 - 它应该只加载它所拥有的那些。你UITableView
的{{1}}是什么?如果这个数字非常低,那么Table可能需要加载所有单元格
如果这不是问题,您可以粘贴rowHeight
代码吗?
答案 3 :(得分:0)
您是否自己调用了cellForRowAtIndexPath,例如来自heightForRowAtIndexPath?如果是这样,您不需要创建单元格来确定其高度。