Data doesn't load in UITableView until I scroll

I try to load the analyzed data into the cells, but the problem is that this happens synchronously and the UitableView is not displayed until the data has finished loading. I tried to solve the problem using the performSelectorInBackground function, but now the data does not load into the cells until the start of scrolling. Here is my code:

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self performSelectorInBackground:@selector(fethchData) withObject:nil]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. self.listData = nil; self.plot=nil; } -(void) fethchData { NSError *error = nil; NSURL *url=[[NSURL alloc] initWithString:@"http://www.website.com/"]; NSString *strin=[[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; HTMLParser *parser = [[HTMLParser alloc] initWithString:strin error:&error]; if (error) { NSLog(@"Error: %@", error); return; } listData =[[NSMutableArray alloc] init]; plot=[[NSMutableArray alloc] init]; HTMLNode *bodyNode = [parser body]; NSArray *contentNodes = [bodyNode findChildTags:@"p"]; for (HTMLNode *inputNode in contentNodes) { [plot addObject:[[inputNode allContents] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; } NSArray *divNodes = [bodyNode findChildTags:@"h2"]; for (HTMLNode *inputNode in divNodes) { [listData addObject:[[inputNode allContents] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; //here you check for PreCreated cell. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } //Fill the cells... cell.textLabel.text = [listData objectAtIndex:indexPath.row]; cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; cell.textLabel.numberOfLines=6; cell.textLabel.textColor=[UIColor colorWithHue:0.7 saturation:1 brightness:0.4 alpha:1]; cell.detailTextLabel.text=[plot objectAtIndex:indexPath.row]; cell.detailTextLabel.font=[UIFont systemFontOfSize:11]; cell.detailTextLabel.numberOfLines=6; return cell; } 
+51
objective-c uitableview cocoa synchronous
Jun 04 2018-12-12T00:
source share
7 answers

Put this somewhere after successfully loading the data :

 dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); 

This fixes the issue of invoking a GUI update until you are in the main thread.

This code uses Apple's GCD technology to make the reload data function work in the main thread. Learn more about the Concurrency Programming Guide for a better understanding (this is a rather large field, so it’s hard to explain in the comment) Anyway, this is not very recommended if you don’t understand it well, because it leads to a program crash in some rare cases.

+123
Jun 18 '13 at 10:03
source share

For quick 3:

 DispatchQueue.main.async(execute: { () -> Void in self.tableView.reloadData() }) 

For quick 2:

 dispatch_async(dispatch_get_main_queue(), { () -> Void in self.tableView.reloadData() }) 
+12
Mar 29 '16 at 10:01
source share

All you really need to do is when you update your source data, call

 [tableView reloadData]; 

Since this happens synchronously, you should probably have a function like

 -(void) updateTable { [tableView reloadData]; } 

and after adding data to your download request

 [self performSelectorOnMainThread:@selector(updateTable) withObject:nil waitUntilDone:NO]; 
+9
Jun 04 '12 at 17:17
source share

U can use [cell setNeedsDisplay]; eg:

 dispatch_async(dispatch_get_main_queue(), ^{ [cell setNeedsDisplay]; [cell.contentView addSubview:yourView]; }); 
+2
May 19 '15 at 8:55
source share

I had the same problem! I wanted the UITableView to be fully populated before the view controller appeared. An Envil post provided me with the information I needed, but my solution turned out to be different.

Here is what I did (reconstructed to fit the context of the question).

 - (void)viewDidLoad { [super viewDidLoad]; [self performSelectorInBackground:@selector(fethchData) withObject:nil]; } - (void)viewWillAppear { [tableView reloadData]; } 
+1
Nov 19 '14 at 19:59
source share

I had this problem and dealt with it all day.

I use static cells and reloadData causes the wrong loading, it only displays visible cells and removes others. I noticed that when I scrolled down (y in a negative value) the cells where it loaded correctly, so I wrote this code and it worked, although I do not like to skip it that way.

Shoot if you find the best solution.

 -(void)reloadTableView{ CGPoint point = self.tableSettings.tableView.contentOffset; [self.tableSettings.tableView reloadData]; [UIView animateWithDuration:.001f animations:^{ [self.tableSettings.tableView setContentOffset:CGPointMake(point.x, -10)]; } completion:^(BOOL finished) { [UIView animateWithDuration:.001f animations:^{ [self.tableSettings.tableView setContentOffset:point]; } completion:^(BOOL finished) { }]; }]; } 
+1
Mar 15 '16 at 10:12
source share

Firstly, this half-solved my problem. I want to round the corners of an image in a table cell. The dispatcher asynchronously fixed the problem for some, but not all images. Any ideas?

Secondly, I think you should avoid creating a strong reference loop by using a close list of captures as follows:

 DispatchQueue.main.async(execute: { [weak weakSelf = self] () -> Void in weakSelf!.tableView.reloadData() }) 

See: https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID52

0
Oct 14 '16 at 0:03
source share



All Articles