如何使UITableView快速加载数据

时间:2014-09-17 10:47:26

标签: ios json uitableview

在我的应用中,tableView数据加载速度非常慢。我从JSON获取图像以在UITableViewCell中显示它。

首先,UITableView为空,仅在15秒后,数据显示出来。我希望在tableView开始加载的同时加载数据。

请告诉我有关我的问题的任何想法。这是一些相关的代码。

- (void)viewDidLoad

{
  NSURL * url=[NSURL URLWithString:@"http://www.educarelive.com/SaveDollar/rest/classifieds/getTestImage"];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    con1=[NSURLConnection connectionWithRequest:req delegate:self];
    if(con1)
    {   NSLog(@"Connected");
        webData=[[NSMutableData alloc]init];
        NSLog(@"Connected2");
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
cell.thumbnailImageView.image = [secondarray objectAtIndex:indexPath.row];        cell.nameLabel.text = [titlearray objectAtIndex:indexPath.row];
}

2 个答案:

答案 0 :(得分:1)

你可以使用SDWebImage,它将在后台进行服务调用,让你更新UI(cell.nameLabel),对于你的图像部分,这将根据连接速度更新。所以这种方式你的用户将能够看到nameLabel和图像(稍后 - 取决于网络的速度)并加载你的tableView更快。

获取图片时也无需调用重新加载。 SDWebImage代码如下: -

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

  // Here we use the new provided setImageWithURL: method to load the web image
 [cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

  cell.nameLabel.text = [titlearray objectAtIndex:indexPath.row];

  return cell;
}

以下是SDWebImage的链接供您参考。

答案 1 :(得分:0)

我非常同意walle84。此外,您可以在处理Image到ImageView时使用UIActivityIndi​​cator。

为此您可以再次使用SDWebImage的类别。以下是此UIActivityIndicator-for-SDWebImage

的链接