【问题标题】:Weird error in UITableView NSInvalidArgumentException reason: [NSNull length], when it is not nullUITableView NSInvalidArgumentException 中的奇怪错误原因:[NSNull 长度],当它不为空时
【发布时间】:2014-04-09 21:27:03
【问题描述】:

我遇到了暗恋问题。这是我在网上找不到解决方案的第一件事。如果您之前遇到过同样的问题,请提供帮助。谢谢。

错误信息:

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

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *lblCategoryName;
    UILabel *lblGroupName;
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        lblCategoryName = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
        [lblCategoryName setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
        [lblCategoryName setTag:1];
        [cell.contentView addSubview:lblCategoryName];

        lblGroupName = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 20)];
        [lblGroupName setFont:[UIFont fontWithName:@"HelveticaNeue-Italic" size:10]];

        [lblGroupName setTag:2];
        [cell.contentView addSubview:lblGroupName];
    }

    POCategory *category = [self.variantCategories objectAtIndex:indexPath.row];

    lblCategoryName = (UILabel*)[cell.contentView viewWithTag:1];
    [lblCategoryName setText:category.CategoryName];

    lblGroupName = (UILabel*)[cell.contentView viewWithTag:2];
    [lblGroupName setText:category.GroupName];

    return cell;
}

奇怪的是; 当我注释掉以下行当我的数组“variant.Categories”为空时,它工作得很好。(我什至可以从其他函数填充表格视图) 然而,它并没有在这些方面粉碎。 我可以在调试时看到该对象。不是空的。一切正常。

    //POCategory *category = [self.variantCategories objectAtIndex:indexPath.row];

    //lblCategoryName = (UILabel*)[cell.contentView viewWithTag:1];
    //[lblCategoryName setText:category.CategoryName];

    //lblGroupName = (UILabel*)[cell.contentView viewWithTag:2];
    //[lblGroupName setText:category.GroupName];

“variantCategories”数组在我的 UITableViewController 类中是这样定义的,这是我传递给这个视图控制器的唯一变量。

    @property (nonatomic, retain) NSArray *variantCategories;

我试图让它“强”而不是“保留”,但没有奏效。

高度赞赏任何形式的帮助。

谢谢。

【问题讨论】:

  • 在 Xcode 中,为异常设置断点。然后你会看到发生这种情况的确切位置。你在某处有一个 NSNull 类的对象。
  • 该消息表明在空对象上调用了getLength 方法。由于很可能会在 NSString 上调用 length 方法,并且注释两个 setText 行可以解决您的问题,因此我猜我会说 GroupName 或 CategoryName 是 NSNull。您应该确保在您的 Category 对象上将这两个属性定义为 strong
  • 是的,问题是 category.GroupName 为空。谢谢 Paulw11
  • 您还应该启用 NSZombies - michalstawarz.pl/2014/02/22/…
  • @Hüseyin Avni YALÇIN :- 这与我的项目所遇到的情况相同,不同之处在于我的表格视图是静态的,当我开始在单元格的 textField 上键入一些内容时出现此错误:( 运气不好请分享你的经验!这里here是我的问题供你参考!

标签: ios objective-c uitableview nsnull


【解决方案1】:

该消息表明在 NSNull 对象上调用了 length 方法。由于很可能会在NSString 上调用长度方法,并且注释两条setText 行可以解决您的问题,因此我猜我会说GroupName 或CategoryName 是NSNull。您应该确保这两个属性在您的 Category 对象上被定义为 strong。

【讨论】:

  • 实际上错误表明调用了length方法,而不是getLength。它是在 NSNull 的实例上调用的,而不是“空对象”。
【解决方案2】:

你能检查一下这段代码,它会起作用吗:

如果(单元格==nil) { 单元格 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIndentifier]; countryLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(tableView.bounds)/2, 44)]; countryLbl.textColor = [UIColor blueColor]; countryLbl.textAlignment = NSTextAlignmentLeft; [cell.contentView addSubview:countryLbl]; [countryLbl setTag:11]; localNoLbl = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(tableView.bounds)/2, 0,CGRectGetWidth(tableView.bounds)/2, 44)]; localNoLbl.textColor = [UIColor blackColor]; localNoLbl.textAlignment = NSTextAlignmentCenter; [cell.contentView addSubview:localNoLbl]; [localNoLbl setTag:12];

    }

    [cell.contentView.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {


        if ([obj isKindOfClass:[UILabel class]]) {
           countryLbl = (UILabel *)[cell.contentView viewWithTag:11];

                countryLbl.text = [countryLocArray objectAtIndex:indexPath.row];
            localNoLbl = (UILabel *)[cell.contentView viewWithTag:12];
                localNoLbl.text=[locArray objectAtIndex:indexPath.row];

            NSLog(@"%@---%@-%@-%@",locArray,countryLocArray,countryLbl.text,localNoLbl.text);
    }
    }];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多