Iphone 在对象数组中使用数据
我有一个名为[self.places objectAtIndex:indexPath.row]的数组;我试图输出到一个表中。数组中充满了对象,但我想使用对象中的数据,将它们分配给对象的最佳方法是什么?数组中的数据如下所示:Iphone 在对象数组中使用数据,iphone,objective-c,ios,xcode,nsarray,Iphone,Objective C,Ios,Xcode,Nsarray,我有一个名为[self.places objectAtIndex:indexPath.row]的数组;我试图输出到一个表中。数组中充满了对象,但我想使用对象中的数据,将它们分配给对象的最佳方法是什么?数组中的数据如下所示: <Place:eXMSFqDg7B> { Location = "<PFGeoPoint: 0x6887480>"; addressline1 = "address line 1"; addressline2 = "addres
<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;
}
地点
#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(我想在其中列出数据)
#导入
#进口
@上课地点;
@ViewController附近的界面:UITableViewController
{
NSArray*地点;
}
@财产(非原子,保留)NSArray*场所;
@结束
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
#导入“NearbyViewController.h”
#进口
#导入“Place.h”
@NearbyViewController的实现
@综合场所;
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
self.title=@“附近”;
PFGeoPoint*userGeoPoint=[PFGeoPoint-geopoint-withlations:37.856965经度:-122.483826];
PFQuery*query=[pfqueryquerywithclassname:@“Place”];
//[query whereKey:@“Location”nearGeoPoint:userGeoPoint within miles:10.0];
[查询whereKey:@“位置”nearGeoPoint:userGeoPoint];
NSArray*placeObjects=[query findObjects];
self.places=placeObjects;
NSLog(@“%i”,[self.places count]);
UIColor*background=[[UIColor alloc]initWithPatternImage:[UIImage ImageName:@“background_texture.png”];
//self.tableView st
self.view.backgroundColor=背景;
[背景发布];
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
返回[self.places count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
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);
返回单元;
}
/*
//替代以支持表视图的条件编辑。
-(BOOL)tableView:(UITableView*)tableView caneditrowatinexpath:(nsindepath*)indepath
{
//如果不希望指定的项可编辑,则返回“否”。
返回YES;
}
*/
/*
//替代以支持编辑表格视图。
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
如果(editingStyle==UITableViewCellEditingStyleDelete){
//从数据源中删除该行
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]和RowAnimation:UITableViewRowAnimationFade];
}
else if(editingStyle==UITableViewCellEditingStyleInsert){
//创建相应类的新实例,将其插入数组,并向表视图添加新行
}
}
*/
/*
//替代以支持重新排列表视图。
-(void)tableView:(UITableView*)tableView移动rowatinexpath:(nsindepath*)从indepath到indepath:(nsindepath*)到indepath
{
}
*/
/*
//重写以支持表视图的条件重新排列。
-(BOOL)tableView:(UITableView*)tableView可以移动rowatinexpath:(nsindepath*)indepath
{
//如果不希望该项目可重新订购,则返回“否”。
返回YES;
}
*/
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
//导航逻辑可能位于此处。创建并推送另一个视图控制器。
/*
*detailViewController=[[alloc]initWithNibName:@“bundle:nil];
// ...
//将选定对象传递给新的视图控制器。
[self.navigationController pushViewController:detailViewController动画:是];
[详细视图控制器发布];
*/
}
@结束
您可以将其与以下内容一起使用
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行中您可以将其与以下代码一起使用
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行中只需创建NSObject类,根据数据将所有字段分别作为字符串和整数,并创建其属性,然后将对象类存储到NSMutableArrayYou需要更具体一点。告诉我们到目前为止你都做了些什么。向我们展示您的代码。只需创建NSObject类,根据您的数据将所有字段分别作为字符串和整数,并创建其正确的