ios - UITableView 滚动滞后

标签 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];
}

最佳答案

您犯了一个非常明显的错误。您正在同步加载图像(在同一个线程上,在本例中是主线程),这就是导致延迟的原因。您应该考虑使用 AFNetworkingUIImageView 类别,它会在后台为您加载这些图像。

您可以从 here 下载 AFNetworking 库.

关于ios - UITableView 滚动滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658606/

相关文章:

javascript - 单击父项时获取子项数据

c# - SCRIPT5009 : 'JSON' is undefined in IE 10 The value of the property '$' is null or undefined, 不是函数对象

iOS - 向 tableview 单元格添加水平填充

ios - 在 ios 中从 jpeg 读取 xmp 元数据

iphone - 如何抹掉UIPasteboard的值(value)

ios - 在 Swift 中批量创建变量

json - Scala Jackson 对象映射器在 case 类中设置 null 而不是默认值

ios - 是否有必要使用单例 CLLocationManager 来避免等待设备位置更新?

ios - 如何处理 UITableViewCell 变得不可见?

ios - 在 View 中使用两个 UITable : how to auto scroll one when the other is scrolled by the user?