草庐IT

ios - UITableView NSInvalidArgumentException 中的奇怪错误原因 : [NSNull length], 当它不为空时

coder 2024-01-25 原文

我有一个迷恋问题。这是我在网上找不到解决方案的第一件事。如果您之前遇到过同样的问题,请帮忙。谢谢。

错误信息:

* 由于未捕获的异常“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”为空时它工作得很好。(我什至可以从另一个函数填充表格 View ) 然而,它在这些方面并没有被压垮。 并且我在调试时可以看到对象。它不是空的。一切看起来都很正常。

    //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 类中是这样定义的,这是我传递给这个 View Controller 的唯一变量。

    @property (nonatomic, retain) NSArray *variantCategories;

我试图让它变得“强大”而不是“保留”,但它没有用。

非常感谢任何形式的帮助。

谢谢。

最佳答案

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

关于ios - UITableView NSInvalidArgumentException 中的奇怪错误原因 : [NSNull length], 当它不为空时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974268/

有关ios - UITableView NSInvalidArgumentException 中的奇怪错误原因 : [NSNull length], 当它不为空时的更多相关文章

随机推荐