0

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
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • Your output "localStop: {...}" indicates that localStop is a dictionary. However, you are initializing it as an NSMutableArray. – NSWill Apr 30 '15 at 22:15

2 Answers2

1

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 UILabels 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:)

aytunch
  • 1,326
  • 1
  • 16
  • 31
1

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)

Community
  • 1
  • 1
Gil Sand
  • 5,802
  • 5
  • 36
  • 78
  • Many thanks to both of you. It turned out to be a problem with naming a custom cell class. I forgot that I was calling a custom cell. But you both gave me ideas to take a fresh look, and I appreciate that. – ICL1901 Apr 30 '15 at 23:25
  • Good that you fixed your issue ;) – Gil Sand Apr 30 '15 at 23:30