Question

I have a UITableView that loads content from a server. All works fine and the content loads correctly, but when I scroll my UITableView lags a bit. I created a method that downloads the content from a server, and I call this method in my viewDidLoad method, since it is the first thing that opens when I open the app. I don't know if this is the best approach. How I can avoid that? Here is my code:

- (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];
}
Was it helpful?

Solution

There's a very obvious mistake you're making. You're loading the image synchronously (on the same thread, which in this case is the main thread), which is what's causing the lag. You should look into using AFNetworking's UIImageView category, which will load these images in the background for you.

You can download the AFNetworking library from here.

OTHER TIPS

I think cause problem by code: 'cell.atracaoImagem.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:programacaoObject.programacaoImagem]]];

you can use SDWebImage framework, loading image

I think you should call the retrieveData function in viewWillAppear method instead of viewDidLoad

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top