-1

I'm trying to pull the JSON data from fields "field_message_subject" and "field_message_body" to a table cell in my app. I'm using the following code to do it, and the data is returned successfully, but my app crashes and the label isn't populated. The crash error is:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

Viewcontroller.m

    - (void)viewDidLoad {
        [super viewDidLoad];


        NSDictionary *entityData = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"1"] forKey:@"uid"];

        [DIOSEntity
         entityGet:entityData
         name:@"entity_message"
         eid:@"uid"
         success:^(AFHTTPRequestOperation *op, id response) {
             self.messages = [NSMutableArray arrayWithObject:(NSDictionary*)response];

             dispatch_async(dispatch_get_main_queue(), ^(void){
                  [self.tableView reloadData];
             });
         }
         failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"failed to get data"); }
         ];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *PointsTableIdentifier = @"MyMessagesCell";

    MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

    NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row];
   [[cell subjectLine] setText:[receivedSubjectLine objectForKey:@"field_message_subject"]];
    NSLog(@"Received message subject is here %@", receivedSubjectLine);

 return cell;

}

See the returned JSON format below:

 2016-02-02 10:37:19.996 app[4208:1406692] Messages are as follows (
            {
            arguments =         (
            );
            data =         (
            );
            "field_message_body" =         {
                und =             (
                                    {
                        format = "<null>";
                        "safe_value" = "Testing message center";
                        value = "Testing message center";
                    }
                );
            };
            "field_message_group_ref" =         (
            );
            "field_message_subject" =         {
                und =             (
                                    {
                        format = "<null>";
                        "safe_value" = Testing;
                        value = Testing;
                    }
                );
            };
            "field_message_user_ref" =         (
            );
            language = en;
            mid = 1;
            "rdf_mapping" =         (
            );
            timestamp = 1447260780;
            type = "private_message";
            uid = 1;
        }
    )
Brittany
  • 1,359
  • 4
  • 24
  • 63

3 Answers3

1

tableview should know how many cell it needs. define this tableviewdatasource method to set number of rows needed.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.messages count];
}

by the way if you increment the number of rows you should initialize your self.messages in viewDidLoad and add entries withh addObject:;

self.messages = [NSMutableArray arrayWithObject:(NSDictionary*)response];

because this will always allocate new space for your array and its size will always be 1.

EDIT

    NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row];

   NSArray * val = receivedSubjectLine[@"und"];
   NSString *textToWrite = val[0][@"value"];
       [cell.subjectLine setText:textToWrite];

to write the text in the field value

meth
  • 1,887
  • 2
  • 18
  • 33
  • I've added return [self.messages count], but now the console throws me the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0x132b87040' when I hit my ViewController... – Brittany Feb 02 '16 at 19:12
  • bacause your `field_message_subject` is not NSString it is also `NSDictionary` – meth Feb 02 '16 at 19:15
  • So shouldn't that line of code I highlighted look something like: ...objectForKey:@"und"] objectForKey:@"value"]; ? I'm just not sure how that line of code would look? – Brittany Feb 02 '16 at 19:17
  • For example I've pulled this data to a UILabel before, just not into a tableview cell, and it looks like: self.messages.text = [[[[[self.comments objectAtIndex:0] objectForKey:@"comment_body"] objectForKey:@"und"] objectAtIndex:0] objectForKey:@"value"]; – Brittany Feb 02 '16 at 19:18
  • Tried your edit - doesn't crash my app, but label is empty... :) ? field_message_subject isn't included? – Brittany Feb 02 '16 at 19:29
  • did you print the `textToWrite` ? it should be `Testing`. if it is not then there is a problem reaching the field. – meth Feb 02 '16 at 19:33
  • Yep - literally copied and pasted exactly as you have above. – Brittany Feb 02 '16 at 19:35
1

Based on your JSON formatting, accessing field_message_subject should be something like

 [[[[receivedSubjectLine objectForKey:@"field_message_subject"] objectForKey:@"und"] objectAtIndex:0] objectForKey:@"value"];
Ryan Friedman
  • 293
  • 3
  • 7
  • This looks closer to what I want, but how do I put the cell in this line (the UILabel I want to populate is called subjectLine and it's located in my TableView cell)? – Brittany Feb 02 '16 at 19:27
  • Use this in cellForRowAtIndexPath like you were doing. You could set the above to an NSString called messageSubject, underneath NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row]. Then, [[cell subjectLine] setText:messageSubject]. – Ryan Friedman Feb 02 '16 at 19:44
0

You are adding response(which is NSDictionary) to self.messages . No where else in your code you are inserting into self.messages. Thus messages array will always hold one value. You are trying to fetch object at row index from messages array. Thus if number of rows are more than 1, your app will crash as messages contain only 1 object.

Vishnu gondlekar
  • 3,896
  • 21
  • 35
  • At the moment, I've set the tableView to only return one row though? – Brittany Feb 02 '16 at 18:57
  • Your crash message says it is trying to access index 1, which would be the second row in your table. Are you sure you are only telling it there is 1 row? – dan Feb 02 '16 at 19:01
  • I've changed it to: I've added return [self.messages count]; but shouldn't the block I've highlighted above look different for retrieving the specified field? eg. something like: ...objectForKey:@"und"] objectForKey:@"value"]; ? I'm just not sure how that line of code would look? – Brittany Feb 02 '16 at 19:14