2016-11-10 56 views
2

我有以下細節異常而顯示在自定義單元格,從詞典數據

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

一本字典,我想表現出細胞的標籤上的名字......對此我做下面的代碼:

-(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 *[email protected]"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; 

} 

我不明白我在代碼中做錯了,因爲它崩潰,並且不顯示在label.It給任何異常

「終止應用程序由於未捕獲的異常‘NSInvalidArgumentException’,原因是:‘ - [__ NSArrayI長]:無法識別的選擇發送到實例0x7bea2d20’」

請檢查代碼,並告訴我,我該怎麼錯在何處!

+0

添加特殊的斷點並查看它在哪裏崩潰。 –

+0

要知道如何添加特殊的突破點,你可以看到http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode。它很容易做到。 –

回答

1

在代碼中存在解析問題。在你的類中使用名爲arrList的全局數組。 請與這一個更新代碼

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

    } 

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

     } 

當我們調用Web服務,陣列會增加字典。

現在改變的代碼行的實現代碼如下:

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

有了這一個:

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

     return [arrList count]; 
    } 

而且我添加幾行代碼中的tableView方法的cellForRowAtIndexPath:

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

    static NSString *[email protected]"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; 

}

現在它會修復你的崩潰。

+0

我試過這段代碼,但它沒有添加任何字典元素到arrlist,並顯示arrlist爲零,而它顯示117個值在字典 – FreshStar

+0

是否有可能顯示我的屏幕截圖或117值的響應..? –

+0

對不起,無法分享字典,但它返回這樣的值117 { Name =「美國航空公司」; 圖片=( 「」 ); Rate = 3; } – FreshStar

相關問題