滚动时IPhone UITableView生涩

时间:2011-12-14 13:29:21

标签: iphone objective-c ios uitableview

我正在将表格从XML Feed加载到数组中,然后按照下面的代码创建单元格。有时装载时可能会非常生涩。我认为这只是我滚动时下载的图像,但是我将它们删除以进行测试,有时它仍然是生涩的。无法弄清楚我做错了什么!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    const NSInteger BOTTOM_LABEL_TAG = 1002;
    const NSInteger TEXT_FIELD_TAG = 1003;

    UILabel *Labelcompanyname;
    UILabel *Labeldistance;

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {

        cell =
        [[[UITableViewCell alloc]
          initWithStyle:UITableViewCellStyleDefault
          reuseIdentifier:CellIdentifier]
         autorelease];


        const CGFloat LABEL_HEIGHT = 15;


        int spaceSize;

        if(currentType == @"categorySearch"){

                spaceSize = 57;
        }
        else {

                spaceSize = 34;
        }


        Labelcompanyname =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     spaceSize + 2.0 * cell.indentationWidth,
                     0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT)+10,
                     tableView.bounds.size.width -
                     spaceSize - 20.0,
                     LABEL_HEIGHT)]
         autorelease];
        [cell.contentView addSubview:Labelcompanyname];


        Labeldistance =[[[UILabel alloc]
                         initWithFrame:
                         CGRectMake(
                                    spaceSize + 2.0 * cell.indentationWidth,
                                    0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT)+40,
                                    tableView.bounds.size.width -
                                    spaceSize - 20.0,
                                    LABEL_HEIGHT)]
                        autorelease];
        if(currentType == @"categorySearch") {
            [cell.contentView addSubview:Labeldistance];

        } 


        Labelcompanyname.tag = BOTTOM_LABEL_TAG;
        Labelcompanyname.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];

        Labeldistance.tag = TEXT_FIELD_TAG;
        Labeldistance.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];

        cell.backgroundView = [[[UIImageView alloc] init] autorelease];
        cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
        cell.textLabel.numberOfLines=0;

    }
    else
    {
        Labelcompanyname = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
        Labeldistance = (UILabel *)[cell viewWithTag:TEXT_FIELD_TAG];   

    }

    PromotionObject *newObj1;
    newObj1=[totalArray objectAtIndex:indexPath.row];

    if(!newObj1.furtherURL){

        Labelcompanyname.text =[NSString stringWithFormat:@"%@", newObj1.companyname];
        Labeldistance.text =newObj1.distance;
    }
    else
    {
        Labelcompanyname.text =[NSString stringWithFormat:@"Load more results"];
        Labeldistance.text =@"";
    }



    NSURL *Imageurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", newObj1.locImage]];
    NSData *data = [NSData dataWithContentsOfURL:Imageurl];
    UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];

    if(currentType == @"categorySearch"){

        cell.imageView.image = img;

    } else if(currentType == @"fullSearch") {

        cell.imageView.image = [UIImage imageNamed:@"newmap.png"];

    } else {

        cell.imageView.image = [UIImage imageNamed:@"business.png"];

    }


    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}   

任何帮助都将非常感激

汤姆

2 个答案:

答案 0 :(得分:3)

在加载单元格时,绝对不应该做任何阻塞UI线程很长时间的事情。例如。 NSData *data = [NSData dataWithContentsOfURL:Imageurl];

我建议您在后台将图像加载到数据源中,并在下载数据时重新加载/更新表格/单元格。为图像实现缓存也很好,这样每次加载单元格时都不必再次下载它们。

如果您需要有关异步图像加载的帮助,请搜索它。很多东西都在SO上。

答案 1 :(得分:1)

我猜你自己得到了答案。您的图片加载会阻止您的UI完成当前的单元格请求。在获取图像之前,tableView:cellForRowAtIndexPath:无法返回。我建议您通过活动指示器替换图像,然后开始在后台获取图像。您可能需要查看Concurrency Programming Guide