UITableView not reloading data. I am using pull to load more library to load data on scroll
I have tried
dispatch_sync(dispatch_get_main_queue(), ^{
[self.pullTableView reloadData];
});
and
[self.pullTableView performSelectorOnMainThread:#selector(reloadData)
withObject:nil
waitUntilDone:YES];
Thanks in advance.
reloadData won't actually fetch data of the internet or your datasource from you, it will just call back the function of numberOfSection numberOfRowInSection: and cellForRowAtIndexPath:
Before calling reload data, you should call the method that go and fetch the new content, then call reloadData
Put your entire class view controller code if you want more help.
Related
I want to create a iOS application with a tableview which show a list string data from API. The data can be update continuously so I want my tableview can update datasource continuously.
This app like exchange rate board in a bank.
Anyone give me a some suggestion? Thank you very much!
If you do not need animation, you can call -(void)reloadDate anytime when your data is updated.
<...>
[self updateDataSource];
[self.tableView reloadData];
<...>
If you need some animation you should use other methods:
<....>
[self.tableView beginUpdate];
[self updateDataSource];
[self.tableView insertRowsAtIndexPaths:<inserted rows index paths> withRowAnimation:<animation>];
[self.tableView endUpdates];
<...>
I have a tableview in a view that lists all the steps that will be executed. I want to show a step as marked when the step was executed. The problem I am facing is that the tableview does not refresh until the procedure executed all the steps. Consequently all the steps show as marked at the same time.
This is my function which starts running when the user presses a button:
-(void)Initialiseer {
//do something for the first step
[self.MyTableView reloadData];
[self.MyView setNeedsDisplay];
//do something for the second step
[self.MyTableView reloadData];
[self.MyView setNeedsDisplay];
//do something for the third step
[self.MyTableView reloadData];
[self.MyView setNeedsDisplay];
}
MyView is the view IBOutlet. MyTableView is the tableview IBOutlet. I also tried [self.view setNeedsDisplay] and [self. MyTableView setNeedsDisplay] but that does not work either. I also tried getting the cell from the tableview and making the changes on the cell itself. Nothing is refreshed until the procedure finished executing completely...
Can someone tell me what I am doing wrong? I am new to iOS development and I searched, read, tried... but did not find an answer so far.
I think you need to read up on the run loop and setNeedsDisplay (which I don't think does what you're expecting). Basically, the UI does not get updated until your code finishes executing. The pattern you need for displaying a complex calculation is something like this:
-(void)Initialiseer {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
//do something for the first step
dispatch_sync(dispatch_get_main_queue(), ^{
[self.MyTableView reloadData];
});
//do something for the second step
dispatch_sync(dispatch_get_main_queue(), ^{
[self.MyTableView reloadData];
});
//do something for the third step
dispatch_sync(dispatch_get_main_queue(), ^{
[self.MyTableView reloadData];
});
});
}
I wanted to highlight a particular row in a tableview(even after refreshing the values in it). But each time when I refresh the table using
[tableView reloadData]
the highlighted selection is gone.
-(void) loadValues {
[tableView reloadData];
[self displaySelection];
}
When I checked the flow of execution, I found out that only after the displaySelection method, the tableView's delegate methods are executed.
I've used a variable as a flag to check for the last row in the 'cellForRow' method to solve my problem. But I really wanted to know is there any other way to check that the tableView's delegate methods are executed completely so that 'displaySelection' will be executed only after it.
Try using something like:
[tableView performSelector:#selector(reloadData) onThread:[NSThread mainThread] withObject:nil waitUntilDone:YES];
This should actually block your thread until the tableview gets reloaded.
I have spent hours searching for the solution with out any luck. I am trying to delete a row (also deselect same row) programmatically. After row deletion call below, UITableViewDelgate methods get called expectedly and data source is updated but UITableView is not refreshed. deselectRowAtIndexPath call also does not work. I tried all kinds of scenarios as shown by commented lines.
Here is my code:
checkoutPerson is called as a result of observer listening for NSNotificationCenter messages.
- (void) checkoutPerson: (NSNumber*) personId {
Person *person = [_people objectForKey:personId];
if( person )
{
// Remove person from data source
int rowIndex = person.rowIndex;
S2Log(#"Deleting row number=%d", rowIndex);
[_allKeys removeObjectAtIndex:rowIndex];
[_people removeObjectForKey: personId];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
//[[self tableView] beginUpdates];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
S2Log(#"Deleting indexPath row=%d", [indexPath row]);
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//[[self tableView] endUpdates];
S2Log(#"Reloading data");
//[[self tableView] reloadData];
//[self performSelector:#selector(refreshView) withObject:nil afterDelay:1.5];
//[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:YES];
}
}
I will appreciate for help.
Thanks
-Virendra
I believe deleted cell is not being recycled. If I delete row in the middle, last row is always erased (since there is one less item) but the deleted row remains.
Use the above code between two function for table view
[tableView beginUpdates];
// the deletion code from data source and UITableView
[tableView endUpdates];
By calling this functions you are telling UITableView that you are about to make updates for deleting your cell.
Edit
The other problem I see with your code is you first delete the data from the data source.
Now you are asking for the UITableViewCell (which actually reloads the UITableView)
and then you are deleting the row from UITableView
I guess you should fetch the UITableViewCell before deleting values from your data source.
I found the problem. It has nothing to do with the code I posted above. It is syncing problem between visual display and the contents of data source. I have an embedded UITableView as part of a composite view. In composite view's controller, I was wiring up UITableView's delegate and data source to an instance of UITableViewController. Instead of this, I should have set UITableViewController's tableView property to the embedded UITableView. It seems that UITableView has to be contained within UITableViewController in order to correctly sync up table view visual display to the contents of data source. This also fixes row deselection and scrolling. I also needed to delay reloadData call in which case deleteRowsAtIndexPaths:withRowAnimation is not required. All you need is to modify the contents of your data source and call reloadData with a delay of 1.5 Seconds.
Thanks to all for great help.
I have a table view with a detail label on the right. This label is populated by a entity that I populate from a JSON call in my connectionDidFinishLoading function. The problem is that the cellFromRowAtIndexPath function fires before the connectionDidFinishLoading function so I don't see the values in my table view until I pull down on the screen which redraws the page. I have tried refreshing the table view at the end of both of these functions but that doesn't see to work. Can someone give me a clue as to why this is happening and how I can fix it please.
you have to reload your table data at the end of your connectionDidFinishLoading.
two ways:
The first:
[tableView reloadData];
The second:
NSArray* rows = [tableView indexPathsForVisibleRows];
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
Are you calling reloadData in the main thread? connectionDidFinishLoading sounds like it's running in a background thread.
After populating, call reloadData on the tableView.
This should do it. All UI updates and manipulations must be performed from the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});