UITableView滚动滞后

时间:2014-05-14 15:11:09

标签: ios json uitableview

我有一个UITableView,可以从服务器加载内容。 一切正常,内容加载正确,但当我滚动我的UITableView滞后一点。 我创建了一个从服务器下载内容的方法,我在viewDidLoad方法中调用了这个方法,因为它是我打开应用程序时打开的第一个东西。 我不知道这是不是最好的方法。 我怎么能避免这种情况?这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Method that Loads the Content
    [self retrieveData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Retorna o número de linhas pelo array de programacao.
    return programacaoArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    // ALTURA da TableViewCell
    return 150;
}

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

    if (cell == nil) {
        cell = [[ProgramacaoTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    // Configure the Cell
    Programacao *programacaoObject;
    programacaoObject = [programacaoArray objectAtIndex:indexPath.row];

    cell.atracaoImagem.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:programacaoObject.programacaoImagem]]];
    cell.nomeLabel.text = programacaoObject.programacaoNome;
    cell.dataLabel.text = programacaoObject.programacaoData;
    cell.localLabel.text = programacaoObject.programacaoLocal;

    return cell;

}

- (void) retrieveData
{

    NSURL *url = [NSURL URLWithString:getDataURL];
    NSData *data = [NSData dataWithContentsOfURL:url];

    jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    // SETUP programacaoArray
    programacaoArray = [[NSMutableArray alloc] init];

    // Loop throught do Array
    for (int i = 0; i < jsonArray.count; i++) {
        // Cria o PROGRAMACAO Objeto
        NSString *pID         = [[jsonArray objectAtIndex:i] objectForKey:@"id"];
        NSString *pNome       = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoNome"];
        NSString *pImagem     = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoImagem"];
        NSString *pDescricao  = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoDescricao"];
        NSString *pData       = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoData"];
        NSString *pLocal      = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoLocal"];
        NSString *pPrecos     = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoPrecos"];

        // Add to the Array
        [programacaoArray addObject:[[Programacao alloc] initWithNome:pNome andImagem:pImagem andDescricao:pDescricao andData:pData andLocal:pLocal andPrecos:pPrecos andID:pID]];
    }

    // RELOAD TABLE VIEW
    [self.tableView reloadData];
}

3 个答案:

答案 0 :(得分:2)

你犯了一个非常明显的错误。您同步加载图像(在同一个线程上,在本例中是主线程),这是造成延迟的原因。您应该考虑使用AFNetworking的{​​{1}}类别,它会在后台加载这些图片。

您可以从here下载UIImageView库。

答案 1 :(得分:0)

我觉得代码导致问题: &#39; cell.atracaoImagem.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:programacaoObject.programacaoImagem]]];

您可以使用SDWebImage框架,加载图片

答案 2 :(得分:-1)

我认为您应该使用 retrieveData 方法而不是viewWillAppear

来调用viewDidLoad函数