UITableView滚动感觉很杂乱

时间:2011-09-14 00:06:14

标签: objective-c ios cocoa-touch uitableview

我有一个tableView,我将IOILabel添加到cell contentView作为cellForRowAtIndexPath方法中的子视图,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ReuseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil ){
    cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Code to remove labels overlay
for(UIView* view in cell.contentView.subviews){
    if ([view isKindOfClass:[UIView class]]) {
        [view removeFromSuperview];
    }
}
if ([indexPath row]<[itemsArray count]){
    UILabel* label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,18,280,10)];
    label1.text = [NSString stringWithFormat:@"Description %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Desc"]];
            label1.font = [UIFont fontWithName:@"Helvetica" size:14];
    [cell.contentView addSubview:label1];
    [label1 release];

    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10,25,280,10)];
    label2.text = [NSString stringWithFormat:@"Price %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Price"]];
    label2.font = [UIFont fontWithName:@"Helvetica" size:14];
    [cell.contentView addSubview:label2];
    [label2 release];

    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,40,290,11)];
    label3.text = [NSString stringWithFormat:@"Loc %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Loc"]];
    label3.font = [UIFont fontWithName:@"Helvetica" size:14];
    [cell.contentView addSubview:label3];
    [label3 release];
}
return cell;

}

当我滚动表格视图时,由于单元格重用,UILabels在前一个上重叠,因此我使用以下方法将其删除:

// Code to remove labels overlay
    for(UIView* view in cell.contentView.subviews){
        if ([view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }

如果要显示许多行,那么我的滚动不是那么顺利的原因。如何使我的滚动顺畅?

1 个答案:

答案 0 :(得分:2)

不要删除标签:重复使用它们。如果您为每个人分配tag,则可以使用内容视图的-viewWithTag:检索它们,然后只需更改其文字。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ReuseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    if( cell == nil )
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,18,280,10)];
        label1.font = [UIFont fontWithName:@"Helvetica" size:14];
        label1.tag = 1;
        [cell.contentView addSubview:label1];
        [label1 release];

        label2 = [[UILabel alloc] initWithFrame:CGRectMake(10,25,280,10)];
        label2.font = [UIFont fontWithName:@"Helvetica" size:14];
        label2.tag = 2;
        [cell.contentView addSubview:label2];
        [label2 release];

        label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,40,290,11)];
        label3.font = [UIFont fontWithName:@"Helvetica" size:14];
        label3.tag = 3;
        [cell.contentView addSubview:label3];
        [label3 release];
    }
    else
    {
        // retrieve the labels using the tags you defined earlier

        label1 = (UILabel *)[cell.contentView viewWithTag:1];
        label2 = (UILabel *)[cell.contentView viewWithTag:2];
        label3 = (UILabel *)[cell.contentView viewWithTag:3];
    }

    if ( [indexPath row] < [itemsArray count] )
    {
        NSDictionary *item = [itemsArray objectAtIndex:[indexPath row]];
        label1.text = [NSString stringWithFormat:@"Description %@", [item valueForKey:@"Desc"]];
        label2.text = [NSString stringWithFormat:@"Price %@", [item valueForKey:@"Price"]];
        label3.text = [NSString stringWithFormat:@"Loc %@", [item valueForKey:@"Loc"]];
    }

    return cell;
}