2

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

错误信息:

*由于未捕获的异常“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;

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

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

谢谢。

4

2 回答 2

1

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

于 2014-04-09T21:40:47.110 回答
0

您可以检查此代码是否可以使用:

if (cell==nil) { cell = [[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);
    }
    }];
于 2015-10-06T04:38:49.800 回答