UITableview in UITableviewcontroller - cellforrowatindexpath not called on reboot

I am stuck in a stupid problem since two days. I have a UITableViewController clicked in the Navigation Controller . When it loads, since there is no data, an empty table is visible:

But when I get data from the server and call [self.tableView reloadData] , both numberOfRowsInSection and heightForRowAtIndexPath receive a call, except cellForRowAtIndexPath , and my controller is displayed without a table:

I can’t understand why this is happening. All data source methods are called with the exception of cellForRowAtIndexPath . Please help me ... Thanks ..

ActLogController.h

 @interface ActLogController : UITableViewController<ASIHTTPRequestDelegate,UITableViewDataSource,UITableViewDelegate> @property(strong) NSMutableArray *activityKeys; @end 

ActLogController.m

 - (void)viewDidLoad { [super viewDidLoad]; activityKeys = [[NSMutableArray alloc] init]; self.tableView.dataSource = self; } -(void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self retrieveActivityLogFromServer]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return activityKeys.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... ActivityLogUnit *act = [activityKeys objectAtIndex:indexPath.row]; cell.textLabel.text = act.price; return cell; } -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50.0; } -(void) requestFinished:(ASIHTTPRequest *)request { NSArray *list = [request.responseString JSONValue]; for (int i = 0; i < list.count; i++) { NSArray *singleTrade = [[list objectAtIndex:i] JSONValue]; ActivityLogUnit *unit = [[ActivityLogUnit alloc] init]; unit.symbol = [singleTrade objectAtIndex:0]; unit.type = [singleTrade objectAtIndex:1]; unit.price = [singleTrade objectAtIndex:2]; unit.volTraded = [singleTrade objectAtIndex:3]; unit.remVol = [singleTrade objectAtIndex:4]; unit.actualVol = [singleTrade objectAtIndex:5]; unit.recordID = [singleTrade objectAtIndex:6]; unit.orderNo = [singleTrade objectAtIndex:7]; unit.time = [singleTrade objectAtIndex:8]; [activityKeys addObject:unit]; } if(activityKeys.count > 0) { [self.tableView reloadData];//it is called and I have got 6 items confirm } } 

EDIT

I set some dummy data in my activityKeys array, the data is displayed in the table, and cellForRowAtIndexPath is called successfully . But since I reload the data after some time, other methods are called, besides this, and the table disappears, as shown in the 2nd figure. Any ideas?

+4
source share
7 answers

I finally worked. cellForRowAtIndexPath not called due to a line of code that I didn’t mention here ... which actually removed some kind of color background layer from the view. This caused a reboot problem. After removing it, everything works fine.

Thank you all for your cooperation :)

0
source

Your problem is that you are likely to load the contents of the data into the background stream. Since you cannot update the interface in the background, you need to call [self.tableView reloadData] in the main stream after the download is complete!

Hope this helps!

+5
source

Looks like you in a secondary thread, do reloadData in the main thread using the following code

 [self.tableView performSelectorOnMainThread@selector (reloadData) withObject:nil waitUntilDone:NO] 

You can always use [NSThread isMainThread] to check if you are in the main thread or not.

+5
source

you need to write in viewdidload

 self.tableView.dataSource = self; self.tableView.delegate = self; 

Edit

You do not have xib, then you declare / set tableview properties. how

 self.tableView.frame = CGRectMake(0, 45, 320, 500); self.tableView.rowHeight = 34.0f; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self.view addSubview:self.tableView]; [self.tableView setBackgroundColor:[UIColor clearColor]]; self.tableView.showsVerticalScrollIndicator=NO; self.tableView.dataSource = self; self.tableView.delegate = self; 

Try

  @property(nonatomic,strong) NSMutableArray *activityKeys; 
+4
source

First, I am convinced that the tableview instance name should not be like a local variable (i.e. the tableView in the class should not be equal to the tableView in the delegate and data source methods).

The second in your question. I do not see the delegate set to present the table. Answer Posted by Samir Rathod, should work if you have @property to represent the table in a .h or .m file.

You can also do this if you have a XIB file. Press ctrl and click + drag the table into the file owner file and set the delegate and data source.

+1
source

For me, the problem was that my truncated code returned 0 as the number of sections (so he never asked how many lines were in the section, and never got their data). Just change this to 1 if that is your problem. Also, I worked in Swift, where the problem mentioned by @ shahid-rasheed is encoded (slightly) differently:

 dispatch_async(dispatch_get_main_queue(), { self.tableView.reloadData() }) 
+1
source

I had the same symptoms. In my case, the first time I loaded data (from the main data) into viewDidLoad, an NSSortDescriptor was used to sort the data.

When the button was clicked, the main data was retrieved again (this time with changes) and the data in the View table was reloaded. Initially, he gave me an empty view in the table after clicking the button, because I forgot to sort the data the second time I typed them.

Training points: do not forget to call all methods that change the cell (for example, the background color mentioned by iAnum or NSSortDescriptor in my case), if you used them at the beginning!

0
source

Source: https://habr.com/ru/post/1491053/


All Articles