Lovely Lovely - 26 days ago 16
iOS Question

Exception while showing data from dictionary on custom cell

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!

Answer

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.

Comments