Guest User

Untitled

a guest
Jan 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. static NSString *CellIdentifier = @"cell";
  4.  
  5. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  6.  
  7. if (cell == nil) {
  8.  
  9. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  10. }
  11.  
  12. NSDictionary *post = [posts objectAtIndex:indexPath.row];
  13.  
  14. cell.textLabel.text = [post objectForKey:@"post_text"];
  15. cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];
  16.  
  17. NSString *postpictureUrl = [post objectForKey:@"picture"];
  18. NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];
  19.  
  20. cell.imageView.image = [UIImage imageWithData:data];
  21.  
  22. return cell;
  23. }
  24.  
  25. Use lazy loading in table view
  26. Use this code
  27.  
  28.  
  29. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  30. {
  31. static NSString *CellIdentifier = @"cell";
  32.  
  33. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  34.  
  35. if (cell == nil) {
  36.  
  37. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  38. }
  39.  
  40. NSDictionary *post = [posts objectAtIndex:indexPath.row];
  41.  
  42. cell.textLabel.text = [post objectForKey:@"post_text"];
  43. cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];
  44. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
  45. dispatch_async(queue, ^{
  46. //This is what you will load lazily
  47. NSString *postpictureUrl = [post objectForKey:@"picture"];
  48. NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];
  49. dispatch_sync(dispatch_get_main_queue(), ^{
  50.  
  51. cell.imageView.image = [UIImage imageWithData:data];
  52. [cell setNeedsLayout];
  53. });
  54. });
  55.  
  56. return cell;
  57. }
Add Comment
Please, Sign In to add comment