【问题标题】:Exception while showing data from dictionary on custom cell在自定义单元格上显示字典中的数据时出现异常
【发布时间】:2016-11-10 07:00:13
【问题描述】:

我有一本包含以下详细信息的字典

{
    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 *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;

}

我无法理解我在代码中做错了什么,因为它崩溃并且标签上没有显示任何内容。它给出了异常

“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSArrayI 长度]:无法识别的选择器发送到实例0x7bea2d20'”

请检查代码并告诉我哪里出错了!

【问题讨论】:

标签: ios dictionary custom-cell


【解决方案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];

        }

当我们调用webservice时,数组会添加字典。

现在更改 tableview 的代码行:

-(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 *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;

}

现在它将解决您的崩溃问题。

【讨论】:

  • 我尝试了这段代码,但它没有将字典的任何元素添加到 arrlist 并将 arrlist 显示为 nil 而它在字典中显示 117 个值
  • 能不能给我看截图或者117个值的回复..?
  • 抱歉,无法共享字典,但它会返回 117 个值,如下所示{ Name = "American Airlines";图片 = ("");率 = 3; }
  • 好的,np...请添加 NSLogs 以查看这些 dicts 是否正确添加...在这种情况下: if([notification.name isEqualToString:@"NewNotification"]) { }
  • 我有 NSLog 字典和 arrlist ....字典中有 117 个元素,但这些元素没有被添加到 arrlist,因此 arrlist 为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2017-09-05
相关资源
最近更新 更多