Reputation: 7778
I'm having a hair pulling problem in a UITableView. I'm just trying to pick out a name field from an array. I must be doing something dumb, but I can't see it. Any ideas please..
NSMutableArray *localStop = [localArray objectAtIndex:indexPath.row];
NSLog(@"localStop: %@", localStop);
BOTH OF THESE RETURN NULL
cell.nameLabel.text = [[localArray objectAtIndex:indexPath.row] valueForKey:@"Name" ];
OR
cell.nameLabel.text = [localStop valueForKey:@"Name"];
NSLog(@"localStop Name1: %@", cell.nameLabel.text);
NSLog(@"localStop Name2: %@", [[localArray objectAtIndex:indexPath.row] valueForKey:@"Name"]);
HOWEVER THE LATTER SHOWS NULL IN THE CELL, BUT DISPLAYS CORRECTLY IN THE CONSOLE.
CONSOLE OUTPUT:
localStop: {
Lat = "38.928922";
Lon = "-77.037531";
Name = "MT PLEASANT ST NW + IRVING ST NW";
Routes = (
42,
43
);
StopID = 1002005;
}
localStop Name1: (null)
localStop Name2: MT PLEASANT ST NW + IRVING ST NW
Upvotes: 0
Views: 30
Reputation: 6040
Your problem probably lies in valueForKey:
when you take data out of the array.
You need to tell us/know/act according to the type of data inside the array.
Usually you'd have custom objects when it comes to cell-filling arrays, so our first example would be custom objects that have properties you display, say
We have a Student objet, that has a name and an age for example, and you'd just have :
Student *currentStudent = [localArray objectAtIndex:indexPath.row];
cell.nameLabel.text = currentStudent.name;
cell.ageLabel.text = currentStudent.age.stringValue;
If you have an NSDictionary
in your array, then it's pretty much the same, except that you extract an NSDictionary
out of the array and use the -objetForKey:
method to retrieve the different keys you need.
Make sure to note the difference between objectForKey and valueForKey
You could also skip the part where you create an object from the array and straight up reference the strings from the array, like so
for the student example :
cell.nameLabel.text = [[localArray objectAtIndex:indexPath.row]name];
or the dictionary :
cell.nameLabel.text = [[localArray objectAtIndex:indexPath.row]objectForKey:@"name"];
Reworking your code following these instructions should solve your problem. If it doesn't, please provide more information about what is in the array and comment here to notify me :)
After looking at the NSLog
, it really appears that you have an array of dictionaries (which is fine), so you have to adapt your code using objectForKey
and treating what's in your array as an NSDictionary
and definitly not as an NSMutablArray
, as suggested by NSWill (in the comments)
Upvotes: 1
Reputation: 1434
You declared
NSMutableArray *localStop
as an array and when you call valueForKey
on an array it should return an NSArray
. Not a NSString
. (is localArray
a two-dimensional array??)
I think you should declare localStop
as NSMutableDictionary
But of course I am not totally sure if this is the case since I don't know what kind of data is contained inside localArray
.
However, if localArray
is a NSDictionary
Array then I don't know why this returns NULL:
cell.nameLabel.text = [[localArray objectAtIndex:indexPath.row] valueForKey:@"Name"];
If localArray
is a two-dimensional array then it's obvious why there is a problem. You are trying to set a UILabel
s text attribute which is a NSString
with a NSArray
(still you should not see NULL but the content of the array as a string) I am confused too:)
Upvotes: 1