2011-12-14 50 views
0

我正在加载我的表从一个XML提要到一个数组,然后创建单元格,如下面的代码显示。有时加载时可能非常不稳定。我认为这只是图片下载时我滚动,但我删除了他们测试,有时它仍然生涩。无法弄清楚我做错了什么!iPhone UITableView生涩时滚动

- (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 [email protected]""; 
    } 



    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; 
} 

任何帮助将是非常赞赏

汤姆

回答

3

你绝对不应该做任何块显著时间UI线程同时加载细胞。例如。 NSData *data = [NSData dataWithContentsOfURL:Imageurl];

我建议在后台将图像加载到数据源中,并在数据下载完成后重新加载/更新表格/单元格。对你的图像实现一个缓存也是很好的,所以你不必在每次加载单元格时再次下载它们。

如果您需要异步映像加载帮助,请搜索该文件。很多东西在SO上。

+1

如果您使用http://afnetworking.org的imageView类别,它会为您处理所有这些 – adamweeks

+0

感谢您的支持,我会着重考虑它 – TMB87

+0

+1 afnetworking.org看起来很不错,而且会非常有用。 –

1

我想你自己得到了答案。您的图片加载会阻止您的用户界面完成当前的单元格请求。 tableView:cellForRowAtIndexPath:只有在获取图像后才能返回。我建议你用活动指示符来替换你的图像,并开始在后台取回你的图像。您可能想看看Concurrency Programming Guide