1

我有一个名为 [self.places objectAtIndex:indexPath.row] 的数组;我正在尝试输出到表格中。数组中充满了对象,但我想使用对象内部的数据将它们分配给对象的最佳方法是什么?数组里面的数据如下:

<Place:eXMSFqDg7B> {
    Location = "<PFGeoPoint: 0x6887480>";
    addressline1 = "address line 1";
    addressline2 = "address line 2";
    agerestrictions = "18+";
    email = "test@aol.com";
    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 = "test@aol.com";
    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 = "test@aol.com";
    image = "<PFFile: 0x6889880>";
    name = Laverys;
    phone = 0123456789;
    postcode = BT11TG;
    type = Club;
}

谢谢。

编辑 - -

正如建议的那样,创建了一个 NSObject 并分别设置字段但收到此错误: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] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[PFObject Location]:无法识别的选择器发送到实例0x6b4e4a0'*第一次抛出调用堆栈:(0x19d5052 0x1f89d0a 0x19d6ced 0x193bf0034144x 0xc01e0f 0xc02589 0xbeddfd 0xbfc851 0xba7301 0x19d6e72 0x1a992d 0x1b3827 0x139fa7 0x13bea6 0x1c730c 0xb79530 0x19a99ce 0x1940670 0x190c4f6 0x190bdb4 0x190bccb 0x251f879 0x251f93e 0xb68a9b 0x2722 0x2695)终止叫做抛出exceptionCurrent

我的代码如下:

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

地点.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(我想列出数据的地方)

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

@class Place;

@interface NearbyViewController : UITableViewController
{
    NSArray * places;
}

@property (nonatomic,retain) NSArray * places;

@end

表视图.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
4

1 回答 1

7

您可以将其与以下一起使用

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

给出indexPath.row后,使用valueForKey:方法从对象中检索值。为所有价值去做,你会得到它。

要在 tableview 中打印,请在CellForRowAtIndexPath中编写上述代码

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

使用此代码,所有 address1 数据都将显示在 tableview 行内

于 2012-04-17T10:12:50.903 回答