滚动时,UITableViewCell会一次又一次地被分配

时间:2011-07-06 05:49:12

标签: iphone uitableview scroll

如果已分配UITableViewCell,则在滚动时,不应重新分配。然而,它。我该如何防止这种情况发生?

1 个答案:

答案 0 :(得分:0)

如果你想重用这个单元格,那么在cellForRowAtIndexPath中编写类似的东西

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) 
    {
     CGRect CellFrame;

        CGRect nameLabelFrame; 

        CGRect countLabelFrame;

        CellFrame = CGRectMake(0, 0, 320, 80);

        nameLabelFrame = CGRectMake(10, 10, 270, 50);
        countLabelFrame = CGRectMake(10, 58, 270, 12);


        cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
        UILabel *lblTemp;
        //UIImageView *imgTemp;

        lblTemp = [[UILabel alloc] initWithFrame:nameLabelFrame];
        lblTemp.tag = 1;
        [cell.contentView addSubview:lblTemp];
        [lblTemp release];



        lblTemp = [[UILabel alloc] initWithFrame:countLabelFrame];
        lblTemp.tag = 3;
        [cell.contentView addSubview:lblTemp];
        [lblTemp release];

}

//And outside of if condition access these labels by this

    //UIConstruction
        UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];

        UILabel *countLabel = (UILabel *)[cell viewWithTag:3];

并在条件

之外使用这些标签或控制你想要的东西