I'm sitting on this almost 4 days and I can't find the problem (already searched google a lot and tried everything but nothing helped). I have this table view I created on my storyboard. Everything is connect BUT when I run my code:
First all the tableView method runs but since array still nil nothing happened. Then after my array got all the data and code says [tableView1 reloadData]; nothing happened and I'm not getting to the tableView methods... (I tried to locate the reloadData in many places in my code and nothing worked).
-(void)viewDidLoad {
[super viewDidLoad];
self.tableView1.dataSource=self;
self.tableView1.delegate=self;
self.tripNamesList = [[NSMutableArray alloc] init];
[self GetTripsList];
PFQuery *query = [PFQuery queryWithClassName:#"Trips"];
NSString *userID = user.objectId;
[query whereKey:#"User_Created_ID" equalTo:userID];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Successfully retrieved %d trips.", objects.count);
[self.tripNamesList addObjectsFromArray:objects];
NSLog(#"%#", tripNamesList);
for (PFObject *object in objects) {
NSLog(#"%#", object.objectId);
[self.tripNamesList addObject:object ];
}
[tableView1 reloadData];
}
else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tripNamesList count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Calling 1222rrgdfghgfhdgfdfgfdgdfgd on %#", tableView);
static NSString *CellIdentifier = #"TripCell";
UITableViewCell *cell = [self.tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
PFObject *obj2 = [self.tripNamesList objectAtIndex:indexPath.row];
PFQuery *query = [PFQuery queryWithClassName:#"Trips"];
PFObject *searchedItems = [query getObjectWithId:obj2.objectId];
NSString *tempTripName = [searchedItems objectForKey:#"Trip_Name"];
cell.textLabel.text = tempTripName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[self.tableView1 reloadData];
return cell;
}
#end
Try running the reloadData from the main thread:
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Successfully retrieved %d trips.", objects.count);
[self.tripNamesList addObjectsFromArray:objects];
NSLog(#"%#", tripNamesList);
for (PFObject *object in objects) {
NSLog(#"%#", object.objectId);
[self.tripNamesList addObject:object ];
}
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView1 reloadData];
});
}
else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
You dont need to call [tableView reloadData] everywhere. Once this method is called, the tableview's delegates will be called in order like numberOfRowsInSection and based on the number of rows, that much time cellForRowAtIndexPath will be called. So in your case, you have put reloadData in cellForRowAtIndexPath and it will create an infinte loop, I guess.
Also, since you are filling self.tripNamesList in background thread, you can call reloadData in main thread as like below
for (PFObject *object in objects) {
NSLog(#"%#", object.objectId);
[self.tripNamesList addObject:object ];
}
[self performSelectorOnMainThread:#selector(refreshTableData) withObject:nil waitUntilDone:NO];
Yes, for this you need to have a method like this in your .m
- (void) refreshTableData{
[tableView reloadData];
}
Hope this helps.
Try to move your code from viewdidload to viewdidlayout, viewwillappear or viewdidapear, your table might not be initialised yet at that point.
Related
I am working on a app that you can change when a item on the menu is in stock or out of stock.
I have it now so it changes the UISwitch to on or off when it loads the screen. I need each switch to change a NSString in parse that makes it one or zero.One meaning that it is on zero meaning its off.
I am fairly new to objective c and parse so if any one could help me get a start on this problem that would be great!
You might use something like that:
PFQuery *query = [PFQuery queryWithClassName:#"YourClass"];
[query whereKey:#"user" equalTo:[PFUser currentUser]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject * yourClass, NSError *error) {
if (!error) {
// Found yourClass object
[yourClass setObject:isInStock forKey:#"isInStock"];
// Save
[yourClass saveInBackground];
} else {
// Did not find any yourClass object for the current user
NSLog(#"Error: %#", error);
}
}];
NSArray *listObjects = .... (loading from Server) // List of PFObject
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PFObject *object = [listObjects objectAtIndex:indexPath.row];
YourCell *cell = .....
if ([[object valueForKey:#"sandwichesOutofstock"] intValue] == 1)
cell.switch.on = true;
else
cell.switch.on = false;
cell.switch.tag = 500 + index.row;
[cell.switch addTarget:self action:#selector(switchTouch:) forControlEvents:UIControlEventTouchUpInside]
.........
}
(IBAction)switchTouch:(UISwitch *)switch{
long index = switch.tag - 500;
PFObject *object = [listObjects objectAtIndex:index];
if(switch.on)
[object setValue:#"1" ForKey:#"sandwichesOutofstock"];
else{
[object setValue:#"0" ForKey:#"sandwichesOutofstock"];
}
[object saveInBackground];
[self.tableView reloadRowsAtIndexPaths:[NSIndexPath indexPathForRow:index inSection:0] withRowAnimation:UITableViewRowAnimationNone];
}
You could assign a reference of the PFObject to the cell. Then when the switch changes just get the cell's object and make the change.
I have a friendslist view controller that allows the user to see pending friend requests. The isPending method is first used to see if a particular user has a pending friend request with the current user.
-(BOOL)isPending:(PFUser *)user
{
for (PFUser *pending in self.Pending){
if ([pending.objectId isEqualToString:user.objectId]){
return YES;
}
}
return NO;
}
This method is called within cellForRowAtIndexPath method inside the TableViewController. THe problem that I am experiencing is that If I run the query inside the tableViewController users who are pending are properly displayed. When I moved the query to a singleton datasource class, I have setup a delegate method that is called when data is returned from the query.
-(void)pendingFriendsQuarryDidFinishWithData{
self.Pending = [NSMutableArray arrayWithArray:dataHolder.getPendingFriendsList];
[self.tableView reloadData];
}
Calling reloadData does not cause the checkmarks to appear next to the users names.
EDIT: Here is the code for cellForRowAtIndexPath. It does not change with the singleton.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
cell.detailTextLabel.text = user.email;
cell.imageView.image = [UIImage imageNamed:#"Treehouse.png"];
if([self isPending:user]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
What does change is how the data is obtained. Without the singleton here is the code
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
/*
self.currentUser = [PFUser currentUser];
if (self.currentUser != nil){
PFQuery *userQuery = [PFUser query];
PFQuery *pendingUser = [PFUser query];
PFRelation *friendRelation = [self.currentUser relationForKey:#"friendRelation"];
PFQuery *existingFriends = [friendRelation query];
PFQuery *pendingFriends = [PFQuery queryWithClassName:#"FriendRequest"];
userQuery.cachePolicy = kPFCachePolicyCacheThenNetwork;
pendingFriends.cachePolicy = kPFCachePolicyCacheThenNetwork;
[pendingFriends whereKey:#"fromUser" equalTo:self.currentUser.objectId];
[pendingFriends whereKey:#"status" equalTo:#"Pending"];
[userQuery whereKey:#"objectId"doesNotMatchKey:#"toUser" inQuery:pendingFriends];
[userQuery whereKey:#"objectId" doesNotMatchKey:#"objectId" inQuery:existingFriends];
[userQuery orderByAscending:#"username"];
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(#" Error %# %#", error, [error userInfo] );
}else{
NSLog(#"All Users Array Starts Here: %#", objects);
self.allUsers = objects;
[self.tableView reloadData];
}
}];
[pendingUser whereKey:#"objectId" matchesKey:#"toUser" inQuery:pendingFriends];
[pendingUser orderByAscending:#"username"];
[pendingUser findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(#"%# %#", error, [error userInfo]);
}else{
[self.Pending addObjectsFromArray:objects];
NSLog(#"Pending Friends Array Stars here: %#", self.Pending);
}
}];
}
*/
}
With singleton
- (void)viewDidLoad
{
dataHolder = [DataHolder sharedInstance];
dataHolder.delegate = self;
self.friends = [NSMutableArray new];
self.allUsers = [NSMutableArray new];
self.allUsers = [NSMutableArray arrayWithArray:dataHolder.getAllUsersWithQuarry];
self.Pending = [NSMutableArray new];
self.Pending = [NSMutableArray arrayWithArray:dataHolder.getPendingFriendsListWithQuarry];
[super viewDidLoad];
NSLog(#"Now in Connection Editor");
}
-(void)allUsersQuarryDidFinishWithData{
self.allUsers = [NSMutableArray arrayWithArray:dataHolder.getAllUsers];
[self.tableView reloadData];
}
-(void)pendingFriendsQuarryDidFinishWithData{
self.Pending = [NSMutableArray arrayWithArray:dataHolder.getPendingFriendsList];
[self.tableView reloadData];
}
Silly question, did you set the datasource of your tableviewcontroller to point to your singleton datasource?
tableview.datasource = [your singleton class instance]
and make sure that you step into it in the debugger.
Hope this helps.
I'm new to objective-C and I am having an issue populating my UItableview with data I retrieved from Parse.
Here is my query:
- (void)viewDidLoad {
[super viewDidLoad];
PFQuery *query = [PFUser query];
[query orderByAscending:#"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"Error: %# %#", error, [error userInfo]);
} else {
//this is my users array
self.allUsers = objects;
[self.tableView reloadData];
}
}];
I have logged this and can see that I successfully retrieved the data. But when I try to populate my tableView as follows. No data shows up. What am I doing wrong?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.allUsers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
return cell;
}
Thanks in Advance for any help.
You need to implement:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.
When I run the following code, nothing appears on my UITableView. I created a global NSMutableArray for storing the results of a query on Parse, but I can't manage to use that array to load the cells on the UITableView.
Thanks!
#import "ViewController.h"
#import "MenuViewController.h"
#import "Parse/Parse.h"
#interface ViewController ()
#end
#implementation ViewController {
CLLocationManager *locationManager;
}
#synthesize eventTableView;
#synthesize eventTableViewCell;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadEvents];
}
- (void) loadEvents
{
eventNames = [[NSMutableArray alloc] init];
PFQuery *event_query = [PFQuery queryWithClassName:#"Event"];
[event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Successfully retrieved %lu scores.", (unsigned long)objects.count);
for (PFObject *object in objects) {
[eventNames addObject:[object objectForKey:#"event_name"]];
NSLog(#"%#", [object objectForKey:#"event_name"]);
NSLog(#"%lu", (unsigned long)eventNames.count);
}
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [eventNames count];
}
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
NSString *cellText = [NSString stringWithFormat:#"%#",eventNames[indexPath.row]];
NSLog(#"%#", eventNames[indexPath.row]);
cell.textLabel.text = cellText;
return cell;
}
#end
Copy and paste the following loadEvents method
- (void) loadEvents
{
eventNames = [[NSMutableArray alloc] init];
PFQuery *event_query = [PFQuery queryWithClassName:#"Event"];
[event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Successfully retrieved %lu scores.", (unsigned long)objects.count);
for (PFObject *object in objects) {
[eventNames addObject:[object objectForKey:#"event_name"]];
NSLog(#"%#", [object objectForKey:#"event_name"]);
NSLog(#"%lu", (unsigned long)eventNames.count);
}
[eventTableView reloadData];
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
Every time you asynchronously fetch data for table view, you have to reload the whole table or the changed sections.
I'm guessing that the result from findObjectsInBackground returned after the tableview has already displayed. Did the NSLog print out the results that you expected?
One thing to try is to add a
[eventTableView reloadData];
after the for loop , which will tell the tableview to reload the data again.
I'm trying to adapt the use of a UICollectionViewController I'm populating with an Array of local images to get the images from Parse.
So far it's pretty straightforward. My NSArray is filled with the same local image many times:
testImages = [NSArray arrayWithObjects: #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", #"thumbnail.jpg", nil];
On the collectionView:cellForItemAtIndexPath: I do set up my cell (from Storyboard):
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Set up cell identifier that matches the Storyboard cell name
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// Configure the cell to show photo thumbnail
UIImageView *testImageView = (UIImageView *)[cell viewWithTag:100];
testImageView.image = [UIImage imageNamed:[testImages objectAtIndex:indexPath.row]];
return cell;
}
This is working and looks like this:
What I'm trying to do is to replace the locally created array from pictures that I get out of my Photo Class in Parse.
I'm trying to do this on the viewDidLoad method:
PFQuery *query = [PFQuery queryWithClassName:#"Photo"];
PFUser *user = [PFUser currentUser];
[query whereKey:#"user" equalTo:user];
[query orderByAscending:#"createdAt"];
[query setCachePolicy:kPFCachePolicyNetworkOnly];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d photos.", objects.count);
testImages = [NSMutableArray arrayWithArray:objects];
NSLog(#"# Images: %d", [testImages count]);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(#"Object Name: %#", object.objectId);
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
The thing is that I'm getting an empty array every time. I'm guessing that since this is getting executed on the block in the background once the collectionView:numberOfItemsInSection: asks for the count of elements on the "testImages" array I always get 0 elements.
When the UICollectionViewController wants to use the information from the array to fill in the cells there's nothing there.
I don't know if I'm placing my code in the wrong place or if I'm using the wrong query.
Can you get my error here?
Any feedback would be greatly appreciated.
finally I got something working. #gg13's feedback has been key to solve the empty array problem.
I'll leave the solution here just in case it helps anyone else some other time.
I put my query on a new method:
- (void)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:#"Photo"];
PFUser *user = [PFUser currentUser];
[query whereKey:#"user" equalTo:user];
[query orderByAscending:#"createdAt"];
[query setCachePolicy:kPFCachePolicyNetworkOnly];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d photos.", objects.count);
[self.collectionView reloadData];
gridImages = [[NSMutableArray alloc] initWithCapacity:objects.count];
// Do something with the found objects
for (PFObject *object in objects) {
PFFile *thumbnail = [object objectForKey:#"thumbnail"];
[thumbnail getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
// Now that the data is fetched, update the cell's image property with thumbnail
NSLog(#"Fetching image..");
[gridImages addObject:[UIImage imageWithData:data]];
NSLog(#"Size of the gridImages array: %d", [gridImages count]);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
That I'm calling on viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self queryForTable];
}
Then I changed my collectionView:cellForItemAtIndexPath: method to this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Set up cell identifier that matches the Storyboard cell name
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// Configure the cell to show photo thumbnail
UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
//NSLog(#"testImages: %#", [testImages objectAtIndex:indexPath.row]);
imageView.image = [UIImage imageNamed:#"placeholder.jpg"];
imageView.image = [gridImages objectAtIndex:indexPath.row];
return cell;
}
And so far it's working. The app is getting the images from Parse and it looks like this:
I still have to test a few things and specially the scenario with huge amount of images (including a progress indicator or something) but at least to get this already is a good thing.
I'm sure Parse will make their own UICollectionViewController sooner or later as they did with the UITableViewController and things will improve even more.
As said before, thanks for the feedback, I'm leaving this here just in case someone else comes into the same problem and of course open to feedback and suggestions.
Cheers,