Beginner here guys. I already managed to get the text from Parse class to table view. In the same class i have another column where I put the associated image file and somehow i could not get the image file. I will be glad if you can help me with this issue. Parse class name is News and the column i would like to get is imageFile. Here is my code.
.h file
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "TableCell.h"
#interface ViewController : UIViewController <UITableViewDelegate> {
NSArray *events;
}
#property (weak, nonatomic) IBOutlet UITableView *newsTable;
#end
.m file
#import "ViewController.h"
#import "TableCell.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize newsTable;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self performSelector: #selector(retreiveFromParse)];
}
- (void) retreiveFromParse {
PFQuery *retrieveEvents = [PFQuery queryWithClassName:#"News"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
events = [[NSArray alloc] initWithArray:objects];
}
[newsTable reloadData];
}];
}
//*********************Setup table of folder names ************************
//get number of sections in tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
//get number of rows by counting number of folders
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return events.count;
}
//setup cells in tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//setup cell
static NSString *CellIdentifier = #"EventCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFObject *tempObject = [events objectAtIndex:indexPath.row];
cell.TitleLabel.text = [tempObject objectForKey:#"Event"];
return cell;
}
//user selects folder to add tag to
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cell tapped");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
My Cell.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface TableCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
#end
My cell.m
#import "TableCell.h"
#implementation TableCell
#synthesize TitleLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Just after
PFObject *tempObject = [events objectAtIndex:indexPath.row];
cell.TitleLabel.text = [tempObject objectForKey:#"Event"];
Write below lines..
PFFile *imageFile = [tempObject objectForKey:#"image"];
PFImageView *imageView = [[PFImageView alloc] init];
imageView.file = imageFile;
[imageView loadInBackground:^(UIImage *img,NSError *error){
if(!error)
{
UIImageView *yourImageView = [[UIImageView alloc] init];
yourImageView.image = imageView.image;
/*OR*/
cell.imageView.image = imageView.image;
}
}];
This might get you going.
This will fetch the associated file and put it in cell.imageView, which is a PFImageView (the Parse edition of UIImageView):
PFObject *tempObject = [events objectAtIndex:indexPath.row];
cell.TitleLabel.text = [tempObject objectForKey:#"Event"];
PFFile *imageFile = [tempObject objectForKey:#"imageFile"];
if (imageFile) {
cell.imageView.file = imageFile;
[cell.imageView loadInBackground];
} else {
cell.imageView.image = [UIImage imageNamed:#"AvatarPlaceholder.png"];
}
Related
I have been trying to do this for a few days and Im stuck. I am using parse as my backend. I don't want to use parse's own ParseTableView to setup my View. I want to understand what is going on with this basic app before I start a more difficult app. How do I use the data from parse to populate my Table??? Thank you.
//
// ViewController.m
// BraveRetiredNumbers
//
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UITableView *playerTableView;
#property (strong, nonatomic) NSArray *testArrary;
#property (strong, nonatomic) NSString *parseClassName;
#end
#implementation ViewController
#synthesize testArrary;
#synthesize playerArray;
#synthesize playerTableView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self performSelector:#selector(retrieveFromParse)];
self.testArrary = #[#"Apple",#"Banana",#"Peach",#"Grape",#"Strawberry",#"Watermelon"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.playerArray count];
//return [testArrary count];
}
- (void) retrieveFromParse {
PFQuery *query = [PFQuery queryWithClassName:#"Player"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(#"%#\n",objects);
if (!error) {
playerArray = [[NSArray alloc] initWithArray:objects];
}
}];
[playerTableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[playerTableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleIdentifier = #"playerCell";
UITableViewCell *cell = [playerTableView dequeueReusableCellWithIdentifier:SimpleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleIdentifier];
}
//PFObject *tempObject = [playerArray objectAtIndex:indexPath.row];
//NSDictionary *tempDic = [playerArray objectAtIndex:#"playerName"];
cell.textLabel.text = [tempDic objectForKey:#"playerName"];
//cell.textLabel.text = [testArrary objectAtIndex:indexPath.row];
return cell;
}
#end
I'm trying to push the image from selected row to a detailed viewController. This is what I tried the latest. I also tried to get the PFFile from an PFImageView but unsuccessful.
#import "ExploreViewController.h"
#import "DetailExploreViewController.h"
#interface ExploreViewController ()
#property (strong, nonatomic) PFGeoPoint *userLocation;
#property (weak, nonatomic) UIImage *imagine;
#property (weak, nonatomic) NSString *discovery;
#end
- (PFQuery *) queryForTable
{
if (!self.userLocation) {
return nil;
}
PFQuery *query = [PFQuery queryWithClassName:#"HomePopulation"];
[query whereKey:#"geopoint" nearGeoPoint:self.userLocation withinKilometers:15];
query.limit = 25;
//[self getLocation];
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleIdentifier = #"ExploreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
}
cell.textLabel.text = [object objectForKey:#"discovery"];
PFFile *getImage = [object objectForKey:#"imageFile"];
[getImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
cell.imageView.image = [UIImage imageWithData:data];
NSLog(#"hojla: %#", cell.imageView.image);
} else {
cell.imageView.image = [UIImage imageNamed:#"1bar.jpg"];
}
}];
return cell;
}
- (void)tableView:(UITableView *)tableViewer didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableViewer cellForRowAtIndexPath:indexPath];
self.discovery = cell.textLabel.text;
NSLog(#"the text: %#", self.discovery);
self.imagine = cell.imageView.image;
NSLog(#"ajde: %#", self.imagine);
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toDetail"]) {
DetailExploreViewController *destViewController = segue.destinationViewController;
destViewController.theImage = self.imagine;
destViewController.theString = self.discovery;
}
}
DetailExploreViewController.h
#import <UIKit/UIKit.h>
#interface DetailExploreViewController : UIViewController
#property (strong, nonatomic) NSString *theString;
#property (strong, nonatomic) UIImage *theImage;
#end
DetailExploreViewController.m
#import "DetailExploreViewController.h"
#interface DetailExploreViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *theImageView;
#property (weak, nonatomic) IBOutlet UILabel *theLabel;
#end
#implementation DetailExploreViewController
#synthesize theString, theLabel, theImageView, theImage;
- (void)viewDidLoad
{
[super viewDidLoad];
theLabel.text = theString;
theImageView.image = theImage;
}
In your cellForRowAtIndexPath you are creating a new UIImageView to contain your image, but not doing anything with it. Once you have the image you can just assign it to the cell's imageView.image property -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleIdentifier = #"ExploreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
}
cell.textLabel.text = [object objectForKey:#"discovery"];
PFFile *getImage = [object objectForKey:#"imageFile"];
[getImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
cell.imageView.image = [UIImage imageWithData:data];
NSLog(#"hojla: %#", cell.imageView.image);
}
else {
//TODO - Put some placeholder/"not found" image into cell.imageView.image
}
}];
return cell;
}
Then, in your didSelectRowAtIndexPath -
- (void)tableView:(UITableView *)tableViewer didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewer cellForRowAtIndexPath:indexPath];
UILabel *label = cell.textLabel
NSLog(#"the text: %#", label.text);
UIImageView *img = cell.imageView;
NSLog(#"ajde: %#", img.image);
}
Try sending a message to the detail controller instead of using the dot syntax like so:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toDetail"]) {
DetailExploreViewController *destViewController = segue.destinationViewController;
[destViewController setTheImage:self.theImage];
[destViewController setTheString:self.theString];
}
}
In the detail controller implementation file , I would set up the detail view image in the viewWillAppear method instead like so:
-(void)viewWillAppear:(BOOL)animated{
theLabel.text = theString;
theImageView.image = theImage;
}
This works for me.
Thanks to #Paulw11's advice I made it happen like this:
ExploreViewController.m
- (PFQuery *) queryForTable {
if (!self.userLocation) {
return nil;
}
PFQuery *query = [PFQuery queryWithClassName:#"HomePopulation"];
[query whereKey:#"geopoint" nearGeoPoint:self.userLocation withinKilometers:15];
query.limit = 25;
// Calling getData to populate the array at the same time as the tableView is being loaded
[self getData];
return query;
}
//Retrieving objects from Parse and adding them in a NSMutableArray
- (void)getData {
PFQuery *query = [PFQuery queryWithClassName:#"HomePopulation"];
[query whereKey:#"geopoint" nearGeoPoint:self.userLocation withinKilometers:15];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
Detailish *detail = [Detailish new]; // Detailish is a class for the array objects
detail.discovery = [object objectForKey:#"discovery"];
PFFile *getImage = [object objectForKey:#"imageFile"];
[getImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
detail.imagine = [UIImage imageWithData:data];
NSLog(#"Detail imagine: %#", detail.imagine);
}
}];
detail.location = [object objectForKey:#"location"];
[forDetail addObject:detail];
}
}
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject
*)object {
static NSString *simpleIdentifier = #"ExploreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
}
cell.textLabel.text = [object objectForKey:#"discovery"];
return cell; }
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
DetailExploreViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(#"Halo ej: %#", [forDetail objectAtIndex:indexPath.row]);
destViewController.hump = [forDetail objectAtIndex:indexPath.row];
Detailish *details = [forDetail objectAtIndex:indexPath.row];
NSLog(#"discovery: %#", details.discovery);
NSLog(#"image: %#", details.imagine);
} }
DetailExploreViewController.h
#import <UIKit/UIKit.h>
#import "Detailish.h"
#interface DetailExploreViewController : UIViewController
#property (strong, nonatomic) Detailish *hump;
#end
DetailExploreViewController.m
#import "DetailExploreViewController.h"
#interface DetailExploreViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *theImageView;
#property (weak, nonatomic) IBOutlet UILabel *theLabel;
#end
#implementation DetailExploreViewController
#synthesize theLabel, theImageView;
- (void) viewWillAppear:(BOOL)animated
{
theLabel.text = self.hump.discovery;
theImageView.image = self.hump.imagine;
}
Detailish.h
#import <Foundation/Foundation.h>
#interface Detailish : NSObject
#property (strong, nonatomic) NSString *discovery;
#property (strong, nonatomic) NSString *location;
#property (strong, nonatomic) UIImage *imagine;
#end
Detailish.m
#import "Detailish.h"
#implementation Detailish
#synthesize discovery, location, imagine;
#end
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.
I have a problem with the bookstableview.m segue. if you look bellow there where it says PFObject *object = [self.objects objectatindex.indexPath.row] there is an error under "objects" saying "Property objects not found on object of type 'BooksTableViewController'."
here is the rest of the code: Bookstableview.m
#import "BooksTableViewController.h"
#import "BookDetailViewController.m"
#interface BooksTableViewController ()
#end
#implementation BooksTableViewController
#synthesize bookstableview;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(RetrieveDatafromParse)];
}
-(void) RetrieveDatafromParse {
PFQuery * getbooks = [PFQuery queryWithClassName:#"BooksTableView"];
[getbooks findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error) {
Booksarray =[[NSArray alloc] initWithArray: objects];
}
[bookstableview reloadData];
NSLog(#"%#",objects);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return Booksarray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"Cell";
UITableViewCell * cell = [bookstableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil) {
cell = [[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
PFObject * tempObject = [Booksarray objectAtIndex:indexPath.row];
cell.textLabel.text = [tempObject objectForKey:#"Books"];
cell.detailTextLabel.text= [tempObject objectForKey:#"Code"];
return cell;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"booksseg" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject * object = [self.objects objectAtIndex:indexPath.row];
PFFile *file = [object objectForKey:#"BooksTableView"];
[[segue destinationViewController] setFile:file];
}
}
#end
bookstableview.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface BooksTableViewController : UITableViewController <UITableViewDelegate >
{
NSArray * Booksarray;
}
#property (strong, nonatomic) IBOutlet UITableView *bookstableview;
#end
Booksdetailview.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "BooksTableViewController.h"
#interface BookDetailViewController : UIViewController {
}
#property (weak, nonatomic) IBOutlet UIImageView *BookImage;
#property (weak, nonatomic) IBOutlet UILabel *bookTitle;
#property (weak, nonatomic) IBOutlet UILabel *bookDesc;
#property (weak,nonatomic) PFObject *file;
#end
Replace self.objects with Booksarray:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject * object = [Booksarray objectAtIndex:indexPath.row];
PFFile *file = [object objectForKey:#"BooksTableView"];
[[segue destinationViewController] setFile:file];
}
}
Also: normally, only class names are capitalized, while variables and methods begin with a lower-case letter, so _booksArray would be a more appropriate name for the variable. Helps avoid confusion and prevents overlap between class/variable names.
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.