user994991
user994991

Reputation: 47

How to access specific column in NSArray using Xcode

I'm new to iOS programming. I'm trying to bind the specific field from the objects in an array to a UITableView. Here's my code:

NSArray *personInfo; //contains a record with fields: name, address, email

personInfo = [[PersonDatabase database] getAllPersons]; //pulling the record into array

From there, I'm trying to get the field "name" from my array.

cell.textLabel.text = [NSString stringWithFormat:@"%@", [personInfo objectAtIndex: indexPath.row] retain]

Upvotes: 2

Views: 1479

Answers (3)

user968597
user968597

Reputation: 1156

Please check that if you delcare your array in @interface file as //////.h

NSArray *personInfo;
@property(nonatomic ,retain) NSArray personInfo;

and then

in @implementation file add this line

@synthesize personInfo;

hope it works

Upvotes: 0

Dov
Dov

Reputation: 16166

As it seems you have objects in your array, what you may be looking for is the -[NSArray valueForKey:] method (documentation here).

For example:

NSArray *names = [personInfo valueForKey:@"name"];

This should return you an array containing all of the names in the array.

Upvotes: 3

Sam Jarman
Sam Jarman

Reputation: 7377

Are you trying to create a 2D Array?. If so you'll need to call objectAtIndex: twice on it in a nested call, but since you're new I'd suggest breaking down to a few lines so you can see more clearly what is happening.

Also, theres heaps of good code snippets on google for dealing with NSArray and table view.

Upvotes: 0

Related Questions