Segue Won't Set Data - ios

I have a segue between MainViewController and BillTrackerViewController, both of which are UIViewController classes, but when i try to set a #property called event_name in BillTrackerViewController upon segue from MainViewController, the property remains null.
Because of this property, I am unable to use it to property query using Parse, as shown in the code below, and the app ends up crashing.
Code for MainViewController (segue name in question is "toBillTracker"):
#import "MenuViewController.h"
#import "AgendaViewController.h"
#import "BillTrackerViewController.h"
#import "ResearchViewController.h"
#import "Parse/Parse.h"
#interface MenuViewController ()
#end
#implementation MenuViewController {}
#synthesize event_name_label;
#synthesize event_name;
- (void)viewDidLoad
{
event_name_label.text = event_name;
[super viewDidLoad];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"toBillTracker"]) {
BillTrackerViewController *controller=(BillTrackerViewController *)segue.destinationViewController;
controller.event_name = event_name;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#end
And here is the code for BillTrackerViewController.m:
#import "BillTrackerViewController.h"
#import "Parse/Parse.h"
#interface BillTrackerViewController ()
#end
#implementation BillTrackerViewController
#synthesize billView;
#synthesize refreshButton;
#synthesize event_name;
#synthesize event_id;
#synthesize resolution_names;
#synthesize resolution_pro_speakers;
#synthesize resolution_con_speakers;
#synthesize resolution_results;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)loadResolutions:(id)sender
{
resolution_names = [[NSMutableArray alloc] init];
resolution_pro_speakers = [[NSMutableArray alloc] init];
resolution_con_speakers = [[NSMutableArray alloc] init];
resolution_results = [[NSMutableArray alloc] init];
// Find event_id of the event
PFQuery *event_id_query = [PFQuery queryWithClassName:#"Event"];
[event_id_query whereKey:#"event_name" equalTo:event_name];
[event_id_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
event_id = object.objectId;
}
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
// Now, use that event_id to locate all of its resolutions
PFQuery *resolution_query = [PFQuery queryWithClassName:#"Resolution"];
[resolution_query whereKey:#"event_id" equalTo:event_id];
[resolution_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
[resolution_names addObject:[object objectForKey:#"resolution_name"]];
// Sort resolutions in alphabetical order
resolution_names = (NSMutableArray*)[resolution_names sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
}
// After sorting alphabetically, load other resolution data (pro speaker, result, etc.)
for (NSString *resolution_name in resolution_names) {
PFQuery *query = [PFQuery queryWithClassName:#"Resolution"];
[query whereKey:#"resolution_name" equalTo:resolution_name];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
[resolution_pro_speakers addObject:[object objectForKey:#"resolution_pro_speaker"]];
[resolution_con_speakers addObject:[object objectForKey:#"resolution_con_speaker"]];
switch ((int)[object objectForKey:#"resolution_result"]) {
case 0:
[resolution_results addObject:#"In Progress"];
case 1:
[resolution_results addObject:#"Passed"];
case 2:
[resolution_results addObject:#"Failed"];
case 3:
[resolution_results addObject:#"Passed with Amendment(s)"];
default:
[resolution_results addObject:#"In Progress"];
}
}
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) { }
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [resolution_names 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:#"%#\nPro: %#\nCon: %#\nResult: %#",resolution_names[indexPath.row], resolution_pro_speakers[indexPath.row], resolution_con_speakers[indexPath.row], resolution_results[indexPath.row]];
cell.textLabel.text = cellText;
return cell;
}
#end
Thanks so much!

Related

Fetch Specific Files from Parse.com

I am pretty confused on how to fetch specific Columns from a selected Row.
My first View is a UICollectionView that finds all objects from a Class. Once a Cell is selected you are segued to a DetailView containing another UICollectionView that will show files from that selected Row.
How do you set up the Query in order for the second CollectionView to show specific Columns of a specific Row?
Please see image. I need to show image_1, image_2, image_3... etc. in the Second CollectionView!
EDITED CODE:
VestimentaViewController.m
#import "VestimentaViewController.h"
#import "VestimentaDetailViewController.h"
#interface VestimentaViewController ()
#end
#implementation VestimentaViewController
#synthesize imagesCollection, activityIndicator;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self queryParseMethod];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)queryParseMethod {
[activityIndicator startAnimating];
PFQuery *query = [PFQuery queryWithClassName:#"VestimentaView"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
vestimentaFilesArray = [[NSArray alloc] initWithArray:objects];
[imagesCollection reloadData];
}
}];
}
#pragma mark Collection data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section {
return [vestimentaFilesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
VestimentaCell *cell = (VestimentaCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
PFObject *infoObject = [vestimentaFilesArray objectAtIndex:indexPath.row];
cell.storeName.text = [infoObject objectForKey:#"cellTitle"];
cell.storeDescription.text = [infoObject objectForKey:#"descriptionTitle"];
[activityIndicator stopAnimating];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"vestimentaDetail"]){
NSIndexPath *indexPath = [self.imagesCollection indexPathForCell:sender];
PFObject *object = [vestimentaFilesArray objectAtIndex:indexPath.item];
VestimentaDetailViewController *detailViewController = segue.destinationViewController;
detailViewController.vestimenta = object;
}
}
#end
VestimentaViewController.h
#import <UIKit/UIKit.h>
#import "VestimentaCell.h"
#import <Parse/Parse.h>
#interface VestimentaViewController : UIViewController {
NSArray *vestimentaFilesArray;
NSMutableArray *tiendasArray;
}
#property (weak, nonatomic) IBOutlet UICollectionView *imagesCollection;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
#end
VestimentaDetailViewController.m
#import "VestimentaDetailViewController.h"
#import "VestimentaViewController.h"
#interface VestimentaDetailViewController ()
#end
#implementation VestimentaDetailViewController
#synthesize imagesCollection;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self queryParseMethod];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Collection data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [filesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
VestimentaDetailCell *cell = (VestimentaDetailCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
PFFile *imageFile = [self.vestimenta objectForKey:#"image_1"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
cell.imageFile.image = [UIImage imageWithData:data];
}
}];
return cell;
}
#end
VestimentaDetailViewController.h
#import <UIKit/UIKit.h>
#import "VestimentaDetailCell.h"
#import <Parse/Parse.h>
#interface VestimentaDetailViewController : UIViewController {
NSArray *filesArray;
}
#property (weak, nonatomic) PFObject *vestimenta;
#property (weak, nonatomic) IBOutlet UICollectionView *imagesCollection;
#end
This will show the images of each cell Clicked but only image_1... How do I construct an array or dictionary that will show image_1, image_2, image_3 of any clicked cell (in the second or detail view controller)?
You could code down a function which is called on selection of row which will fire a query and put up on imageview(PFImageView) in secondViewController like,
-(void)settingAllImageView
{
PFQuery *query = [PFQuery queryWithClassName:#"ImageClassName"];
/*Could put up condition if require specific row*/
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error && objects.count != 0)
{
NSLog(#"Successfully retrieved: %#", objects);
for(int *i=0;i<=[objects count];i--)
{
NSDictionary *dict = [objects objectAtIndex:i];
PFFile *file = [object objectForKey:#"image"];
imageView.file = file;
/*imageView is PFImageView*/
/*For more than 1 images in a row you could store PFFiles in a array den use that array to load up imageView*/
}
// Now tell PFImageView to download the file asynchronously
[imageView loadInBackground];
}
}];
}
You don't fetch specific columns from Parse. You always fetch the whole row as a PFObject, and then you get specific properties from that object.
In your collection view, you are already showing the cellTitle. This means you already have the PFObject containing the images. You should use a PFImageView to display the images. The files are not downloaded yet, so what you need to do is set the PFImageView file to the image_1 file etc, like this:
PFImageView *firstImage = [[PFImageView alloc] init];
firstImage.file = theObjectFromPreviousCollectionView[#"image_1"];
[firstImage loadInBackground];
"theObjectFromPreviousCollectionView" is the object corresponding to the cell you clicked. In the prepareForSegue method, you pass this object to the detail view.

Parse.com Textview don`t show anything

I want to add some non editable text(it`s some description of toys in my app) via parse and I am stuck with the code.
This part (I think) I should change, but don`t know how.
Thanks
I`ve got it already like this
- (void) retrieveFromParse {
PFQuery *retrieveColors = [PFQuery queryWithClassName:#"Hracky1"];
[retrieveColors findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
colorsArray= [[NSArray alloc] initWithArray:objects];
}
[colorsTable reloadData];
}];
[self.colorsTable reloadData];
[self.refreshControl endRefreshing];
}
This is fully TableViewcontroller.m
#import "TableViewController.h"
#import "CustomCell.h"
#interface TableViewController (){
}
#end
#implementation TableViewController
#synthesize colorsTable;
- (void) retrieveFromParse {
PFQuery *retrieveColors = [PFQuery queryWithClassName:#"Hracky1"];
[retrieveColors findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
colorsArray= [[NSArray alloc] initWithArray:objects];
}
[colorsTable reloadData];
}];
[self.colorsTable reloadData];
[self.refreshControl endRefreshing];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(retrieveFromParse)];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl = refreshControl;
[refreshControl addTarget:self action:#selector(retrieveFromParse)
forControlEvents:UIControlEventValueChanged];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#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 colorsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"colorsCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFObject *tempObject = [colorsArray objectAtIndex:indexPath.row];
[cell.imageview setFile: [tempObject objectForKey:#"ImageURL"]];
[cell.imageview loadInBackground];
cell.cellTitle.text = [tempObject objectForKey:#"cellTitle"];
cell.cellDescript.text = [tempObject objectForKey:#"cellDescript"];
return cell;
}
#end
DetailViewController.m
#import "DetailViewController.h"
#import "Parse/Parse.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)parseview
{
NSString *showText = _textdescript.text;
PFObject *addValues= [PFObject objectWithClassName:#"Hracky1"];
[addValues setObject: showText forKey:#"TextView"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(parseview)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
DetailViewController.h
> #import <UIKit/UIKit.h>
>
> #interface DetailViewController : UIViewController
> <UITextViewDelegate>
>
> #property (nonatomic, strong) IBOutlet UITextView *textdescript; #end
Add a property to the second view controller. Set it when preparing the segue or creating the instance with the identifier.
This code:
- (void)parseview
{
NSString *showText = _textdescript.text;
PFObject *addValues= [PFObject objectWithClassName:#"Hracky1"];
[addValues setObject: showText forKey:#"TextView"];
}
is creating a new empty parse object and setting some text into it. The text is probably nil or #"" because you have only just loaded the view when this happens (though you may have some default text in the XIB).
Most likely that you should be doing is getting an object back from Parse and interrogating it to get the text, then set that onto the view:
PFQuery *query = [PFQuery queryWithClassName:#"Hracky1"];
PFObject *object = [query getFirstObject]; // synchronous request, not ideal, look at getFirstObjectInBackgroundWithBlock
_textdescript.text = [object valueForKey:#"TextView"];
Assuming that you have a class Hracky1 defined in Parse and that it has a string variable named TextView.

Segue Won't Pass Data Onto TableView

I am trying to set an array in another view controller, named BillTrackerViewController, to the values of an NSMutableArray called billtrackertablevalues, located in the main class, MainViewController. I intend to use the array in BillTrackerViewController as data for the UITableView there. However, when I try to set the value of that array, table_items, and segue, the table doesn't show anything. However, the value of billtrackertablevalues is properly filled, and is NOT null.
Here is my code for MainViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"toBillTracker"]) {
PFQuery *query = [PFQuery queryWithClassName:#"Resolution"];
[query whereKey:#"event" equalTo:event_name_label.text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
[billtrackertablevalues addObject:[NSString stringWithFormat:#"%#\nPro: %#\nCon: %#\nResult: %#",[object objectForKey:#"resolution_name"], [object objectForKey:#"resolution_pro_speaker"], [object objectForKey:#"resolution_con_speaker"], [object objectForKey:#"resolution_result_id"]]];
}
NSLog(#"%#", billtrackertablevalues);
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
BillTrackerViewController *controller=(BillTrackerViewController *)segue.destinationViewController;
controller.table_items = [[NSMutableArray alloc] init];
controller.table_items = billtrackertablevalues;
}
}
And here is my code for BillTrackerViewController:
#import "BillTrackerViewController.h"
#import "Parse/Parse.h"
#interface BillTrackerViewController ()
#end
#implementation BillTrackerViewController
#synthesize billView;
#synthesize refreshButton;
#synthesize table_items;
- (void)viewDidLoad
{
[super viewDidLoad];
[billView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) { }
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [table_items 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 = table_items[indexPath.row];
cell.textLabel.text = cellText;
return cell;
}
#end
Once again, the table on BillTrackerViewController doesn't load values, but my initial array, billtrackertablevalues, is filled.
Thanks in advance, guys!
I think you need to move the lines instantiating controller, and setting its property to inside the block method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"toBillTracker"]) {
PFQuery *query = [PFQuery queryWithClassName:#"Resolution"];
[query whereKey:#"event" equalTo:event_name_label.text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
[billtrackertablevalues addObject:[NSString stringWithFormat:#"%#\nPro: %#\nCon: %#\nResult: %#",[object objectForKey:#"resolution_name"], [object objectForKey:#"resolution_pro_speaker"], [object objectForKey:#"resolution_con_speaker"], [object objectForKey:#"resolution_result_id"]]];
}
NSLog(#"%#", billtrackertablevalues);
BillTrackerViewController *controller=(BillTrackerViewController *)segue.destinationViewController;
controller.table_items = billtrackertablevalues;
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
}
After Edit:
There is still a timing issue because the segue happens before the block returns, so you should override the setter for table_items in the BillTrackerViewController. The segue will still occur first (you might want to put a spinner in your view so the user knows something is happening), and then when the block finishes, the setter will be called:
-(void)setTable_items:(NSString *)table_items {
_table_items = table_items;
//update your table here.
}
How about this:
controller.table_items = [[NSMutableArray alloc] initWithArray:billtrackertablevalues];
Also the problem might be in the BillTrackerViewController itself

Parse SDK with iOS Device: Can't Load Data from Query onto UITableView

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.

Segue Won't Pass Data

I am trying to set an array in another view controller, named BillTrackerViewController, to the values of an NSMutableArray called billtrackertablevalues, located in the main class, MainViewController. I intend to use the array in BillTrackerViewController as data for the UITableView there. However, when I try to set the value of that array, table_items, and segue, the table doesn't show anything. However, the value of billtrackertablevalues is properly filled, and is NOT null.
Here is my code for MainViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"toBillTracker"]) {
PFQuery *query = [PFQuery queryWithClassName:#"Resolution"];
[query whereKey:#"event" equalTo:event_name_label.text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
[billtrackertablevalues addObject:[NSString stringWithFormat:#"%#\nPro: %#\nCon: %#\nResult: %#",[object objectForKey:#"resolution_name"], [object objectForKey:#"resolution_pro_speaker"], [object objectForKey:#"resolution_con_speaker"], [object objectForKey:#"resolution_result_id"]]];
}
NSLog(#"%#", billtrackertablevalues);
BillTrackerViewController *controller=(BillTrackerViewController *)segue.destinationViewController;
controller.table_items = [[NSMutableArray alloc] init];
controller.table_items = billtrackertablevalues;
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
}
And here is my code for BillTrackerViewController:
#import "BillTrackerViewController.h"
#import "Parse/Parse.h"
#interface BillTrackerViewController ()
#end
#implementation BillTrackerViewController
#synthesize billView;
#synthesize refreshButton;
#synthesize table_items;
- (void)viewDidLoad
{
[super viewDidLoad];
[billView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) { }
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [table_items 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 = table_items[indexPath.row];
cell.textLabel.text = cellText;
return cell;
}
#end
Once again, the table on BillTrackerViewController doesn't load values, but my initial array, billtrackertablevalues, is filled.
Thanks in advance, guys!
Change the following code:-
controller.table_items = [[NSMutableArray alloc] init];
controller.table_items = billtrackertablevalues;
as like following:-
controller.table_items = [[NSMutableArray alloc] initWithArray:billtrackertablevalues];
After that do NSLog at BillTrackerViewController in viewDidLoad method and let me know. Hope it will work.

Resources