Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Matt Ober
2,038 PointsTableViewCells not updating after Parse.com update?
Hi there. I'm currently making an application that can edit the information stored in certain table view cells by clicking on them and then going to a separate view controller.
Once the changes are made, a save button is pressed, and the fields are updated on parse.com My issue is that once I go back to my main tableViewController, the table cells aren't updating unless I completely restart the application, at which point everything shows as updated.
I've called the [self.tableView reloadData] method like in the iOS tutorials, but it just won't seem to update.
Here's the .m file for the tableViewController that I'm trying to update.
@interface ProspectsTableViewController ()
@end
@implementation ProspectsTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"Prospect"];
[query orderByAscending:@"prospectName"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error in prospect cells");
}
else {
self.allProspects = objects;
NSLog(@"%@", self.allProspects);
[self.tableView reloadData];
}
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.allProspects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ProspectCell";
UITableViewCell *prospectCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *prospect = [self.allProspects objectAtIndex:indexPath.row];
prospectCell.textLabel.text = prospect[@"prospectName"];
return prospectCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"cell pressed");
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"showProspectInfo" sender:cell];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showProspectInfo"]) {
ProspectInfoViewController *transferViewController = segue.destinationViewController; //prepares an instance of the prospectsTableViewController so we can pass our information over to the next view controller
PFObject *prospect = self.allProspects[self.tableView.indexPathForSelectedRow.row]; //selects the information from the prospect at the cell tapped on
transferViewController.name = prospect[@"prospectName"];
transferViewController.phone = prospect[@"phoneNumber"];
transferViewController.email = prospect[@"email"];
transferViewController.objectId = [prospect objectId];
}
}
@end
any ideas? I've been trying to figure this one out for quite some time now
3 Answers
Thomas Nilsen
14,957 PointsUI updates should run on the main thread. Try something like this:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
Matt Ober
2,038 PointsThanks for the advice! So this is going to sound really bad, but where exactly is the main thread of a viewController? I keep seeing it referenced, but I have no idea where it actually is.
Do i need to create it myself?
Thomas Nilsen
14,957 PointsInstead of me giving a lengthy explanation here, I think I'll provide you with a couple of links which should help you out quite a bit.
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html http://stackoverflow.com/questions/7905192/iphone-grand-central-dispatch-main-thread http://en.wikipedia.org/wiki/Thread_(computing)
Sure hope these helps! :)
Matt Ober
2,038 PointsMatt Ober
2,038 PointsThanks for the advice! So this is going to sound really bad, but where exactly is the main thread of a viewController? I keep seeing it referenced, but I have no idea where it actually is.
Do i need to create it myself?