2

I have a dictionary having following details

{
    Name = "American Airlines";
    Picture =         (
            ""
    );
    Rate = 3;
  },

And i want to show the name on label of cell...for which i am doing following code:

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDetail:) name:@"NewNotification" object:nil];

}

-(void)getDetail:(NSNotification *)notification
{

    if([notification.name isEqualToString:@"NewNotification"])
    {
        NSDictionary *dictionary=notification.userInfo;
        NSLog(@"Dictionary  %@",dictionary);
        userInfo=dictionary;
        [self.listView reloadData];

    }


}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //return  [userInfo count];
    return 115;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier=@"cell";
     TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell== nil) {

        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.Name.text =[userInfo valueForKey:@"Name"];

    NSLog(@"the cell.Name.text is %@",cell.Name.text);
    //[cell updateCell:userInfo];
     return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;

}

I can't understand what I'm doing wrong in the code as it crashes and doesn't show anything on label.It give the exception

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7bea2d20'"

Please check the code and tell me where am I going wrong!

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
FreshStar
  • 193
  • 1
  • 1
  • 10
  • add exceptional break point and see where it is crashing. – Ashok R Nov 10 '16 at 07:25
  • to know how to add exceptional break points you can see http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode. its very easy to do. – Ashok R Nov 10 '16 at 07:27

1 Answers1

1

In the code there is a parsing issue. Take a global array in your class called arrList. Please update the code

if([notification.name isEqualToString:@"NewNotification"])
    {
        NSDictionary *dictionary=notification.userInfo;
        NSLog(@"Dictionary  %@",dictionary);
        userInfo=dictionary;
        [self.listView reloadData];

    }

with this one:

if([notification.name isEqualToString:@"NewNotification"])
        {
            NSDictionary *dictionary=notification.userInfo;
            NSLog(@"Dictionary  %@",dictionary);
            [arrList addObject: dictionary];
            [self.listView reloadData];

        }

When we call webservice, array will add dictionary.

Now change the line of code for tableview:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //return  [userInfo count];
    return 115;
}

With this one:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return [arrList count];
    }

And I am adding few lines of code in tableView method cellForRowAtIndexPath:

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

    static NSString *cellIdentifier=@"cell";
     TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     if (cell== nil) {

         cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
   if(arrList.count > 0){
      NSDictonary * dict = arrList[indexPath.row];
      cell.Name.text =[dict valueForKey:@"Name"];

      NSLog(@"the cell.Name.text is %@",cell.Name.text);
      //[cell updateCell:userInfo];
    }
     return cell;

}

Now it will fix your crash.

Gourav Joshi
  • 2,419
  • 2
  • 27
  • 45
  • I tried this code,but it doesn't add any element of dictionary to arrlist and shows arrlist as nil whereas it shows 117 values in dictionary – FreshStar Nov 10 '16 at 09:08
  • Is it possible that you can show me screenshot or response of 117 values..? – Gourav Joshi Nov 10 '16 at 09:10
  • Sorry,it is not possible to share the dictionary but it returns 117 values like this{ Name = "American Airlines"; Picture = ( "" ); Rate = 3; } – FreshStar Nov 10 '16 at 09:12
  • Ok, np...Please add NSLogs to see that these dicts are added properly or not....in this case: if([notification.name isEqualToString:@"NewNotification"]) {} – Gourav Joshi Nov 10 '16 at 09:14
  • I have NSLog the dictionary and arrlist both....the dictionary is filled with 117 elements but these elements are not getting added to arrlist and hence the arrlist is null. – FreshStar Nov 10 '16 at 09:18
  • I think you have not initialized your arrList. Initialize arrList, it will work... – Gourav Joshi Nov 10 '16 at 09:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127777/discussion-between-gourav-joshi-and-lovely). – Gourav Joshi Nov 10 '16 at 09:24