【发布时间】: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