Bradley
Bradley

Reputation: 617

Using data inside an array of objects

I have an array called [self.places objectAtIndex:indexPath.row]; that I am trying to output into a table. The array is full of objects but I want to use the data inside the object whats the best way to assign them to an object? The data inside the array is as follows:

<Place:eXMSFqDg7B> {
    Location = "<PFGeoPoint: 0x6887480>";
    addressline1 = "address line 1";
    addressline2 = "address line 2";
    agerestrictions = "18+";
    email = "[email protected]";
    image = "<PFFile: 0x6887af0>";
    name = "Rain NightClub";
    phone = 0123456789;
    postcode = BT11TG;
    type = Club;
}
2012-04-17 00:29:59.501 ClubsNIparse[3517:f803] <Place:95KvPCrSY1> {
    Location = "<PFGeoPoint: 0x6888330>";
    addressline1 = "address line 1";
    addressline2 = "address line 2";
    agerestrictions = "18+";
    email = "[email protected]";
    image = "<PFFile: 0x6888950>";
    name = "Box NightClub";
    phone = 0123456789;
    postcode = BT11TG;
    type = Club;
}
2012-04-17 00:29:59.502 ClubsNIparse[3517:f803] <Place:6AxpfrY0DZ> {
    Location = "<PFGeoPoint: 0x6889260>";
    addressline1 = "address line 1";
    addressline2 = "address line 2";
    agerestrictions = "18+";
    email = "[email protected]";
    image = "<PFFile: 0x6889880>";
    name = Laverys;
    phone = 0123456789;
    postcode = BT11TG;
    type = Club;
}

Thanks.

EDIT ---

As suggested a created an NSObject and set the fields respectively but received this error: 2012-04-17 11:09:08.363 ClubsNIparse[4906:f803] -[PFObject Location]: unrecognized selector sent to instance 0x6b4e4a0 2012-04-17 11:09:08.364 ClubsNIparse[4906:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFObject Location]: unrecognized selector sent to instance 0x6b4e4a0' * First throw call stack: (0x19d5052 0x1f89d0a 0x19d6ced 0x193bf00 0x193bce2 0x13434 0xc01e0f 0xc02589 0xbeddfd 0xbfc851 0xba7301 0x19d6e72 0x1a992d 0x1b3827 0x139fa7 0x13bea6 0x1c730c 0xb79530 0x19a99ce 0x1940670 0x190c4f6 0x190bdb4 0x190bccb 0x251f879 0x251f93e 0xb68a9b 0x2722 0x2695) terminate called throwing an exceptionCurrent

My Code is as follows:

Place.h (NSObject)

@interface Place : NSObject
{
    NSString * Location;
    NSString * addressline1;
    NSString * addressline2;
    NSString * agerestrictions;
    NSData * image;
    NSString * email;
    NSString * name;
    NSString * phone;
    NSString * postcode;
    NSString * type;

}

@property (nonatomic, retain) NSString * Location;
@property (nonatomic, retain) NSData * image;
@property (nonatomic, retain) NSString * addressline1;
@property (nonatomic, retain) NSString * addressline2;
@property (nonatomic, retain) NSString * agerestrictions;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * postcode;
@property (nonatomic, retain) NSString * type;

-(NSComparisonResult) compareWithAnotherPerson:(Place*) p;

@end

Place.m

#import "Place.h"

@implementation Place

@synthesize name,addressline1,addressline2,email,phone,postcode,type,agerestrictions;
@synthesize Location,image;

-(void) dealloc
{

    self.name = nil;
    self.addressline1 = nil;
    self.addressline2 = nil;
    self.email = nil;
    self.phone = nil;
    self.postcode = nil;
    self.type = nil;
    self.agerestrictions = nil;
    self.Location = nil;
    self.image = nil;


    [super dealloc];


}

-(NSComparisonResult) compareWithAnotherPerson:(Place*) p
{
    return [[self name] compare:[p name]];
}


@end

TableView.h (where I want to list data)

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@class Place;

@interface NearbyViewController : UITableViewController
{
    NSArray * places;
}

@property (nonatomic,retain) NSArray * places;

@end

TableView.m

#import "NearbyViewController.h"
#import <Parse/Parse.h>
#import "Place.h"

@implementation NearbyViewController

@synthesize places;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.title = @"Nearby";
    PFGeoPoint *userGeoPoint = [PFGeoPoint geoPointWithLatitude:37.856965 longitude:-122.483826];
    PFQuery *query = [PFQuery queryWithClassName:@"Place"];
    //[query whereKey:@"Location" nearGeoPoint:userGeoPoint withinMiles:10.0];
    [query whereKey:@"Location" nearGeoPoint:userGeoPoint];

    NSArray *placeObjects = [query findObjects];

    self.places = placeObjects;
    NSLog(@"%i",[self.places count]);

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background_texture.png"]];
    // self.tableView st
    self.view.backgroundColor = background;

    [background release];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#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.places count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    Place*p = [self.places objectAtIndex:indexPath.row];
    NSLog(@"%@",[self.places objectAtIndex:indexPath.row]);
    cell.textLabel.text = [NSString stringWithFormat:@"%@",p.name];
    cell.textLabel.textColor = [UIColor whiteColor];
    NSLog(@"%@",p);
    return cell;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

@end

Upvotes: 1

Views: 890

Answers (1)

iDhaval
iDhaval

Reputation: 7844

You can use it with following

NSString* strAddress = [[self.places objectAtIndex:indexPath.row]valueForKey:@"addressline1"];

After giving indexPath.row use valueForKey: method to retrive value from the object. do it for all value and you will get it.

To print in tableview write above code in CellForRowAtIndexPath

 NSString* strAddress = [[self.places objectAtIndex:indexPath.row]valueForKey:@"addressline1"];
 cell.textLabel.text = strAddress;

Using this code all address1 data will show inside the tableview row

Upvotes: 7

Related Questions