iOS - 名称标签时出现“无法识别的选择器发送到实例”错误

时间:2015-12-18 16:37:15

标签: ios objective-c

当我将表格中的单元格按到另一个视图时,我正在发送一些对象,这些视图必须显示该单元格的详细信息。

当我尝试更改标签(在第二个视图上)时,我得到:

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [UITableViewCell nameOfItem]:   无法识别的选择器发送到实例0x7f8e7ab5ed40'

我的对象:

void in_order(Tree *t) { 
    if (nullptr == t)
        return;
    in_order(t->left);
    process(t);
    in_order(t->right);
}

// preorder and postorder are same except for the order of `process` vs. recursion.

我的手机:

@interface Item : NSObject

@property (strong,nonatomic) NSString * nameOfItem;
@property (strong,nonatomic) NSString * descriptionOfItem;
@property (strong,nonatomic) NSString * categoryOfItem;

-(instancetype)initItem:(NSString *)name category:(NSString *)category description:(NSString *)description;
-(NSComparisonResult)compare:(Item *)otherItem;

@end

我的行选择:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSMutableArray* sortedItems = [[NSMutableArray alloc]initWithArray:[ItemsTable sortItems:self.itemsTable.items]];
    self.itemsTable.items = sortedItems;


    listOfItems = [NSArray arrayWithArray:self.itemsTable.items];
    UITableViewCell *cell;
    NSString *sectionTitle = [[self.itemsTable categoriesOfItemsTable] objectAtIndex:indexPath.section];

    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSInteger index = [indexPath row];


    cell.textLabel.text = ((Item *)[[self.itemsTable listOfItemsInCategory:sectionTitle] objectAtIndex:index]).nameOfItem;


    return cell;

}

我为segue做准备:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *category = [[self.itemsTable categoriesOfItemsTable] objectAtIndex:indexPath.section];//get category of selected item
    Item *testItem = [[self.itemsTable listOfItemsInCategory:category] objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"IntoInfoPage" sender:testItem];
}

和我的第二个观点:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    ...

    if ([segue.identifier isEqualToString:@"IntoInfoPage"]){
        if ([segue.destinationViewController isKindOfClass:[InfoViewController class]]){
            InfoViewController *fivc = (InfoViewController *)segue.destinationViewController;
            fivc.gotchaItem = sender;
        }
    }
}

异常出现在第二个视图的第一行:

@interface InfoViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nameOfFullInfoItem;
@property (weak, nonatomic) IBOutlet UILabel *categoryOfFullInfoItem;
@property (weak, nonatomic) IBOutlet UITextView *descriptionOfFullInfoItem;

@end

@implementation InfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.gotchaItem)
    {
        self.nameOfFullInfoItem.text = [NSString stringWithFormat:@"Name: %@",self.gotchaItem.nameOfItem];
        self.categoryOfFullInfoItem.text = [NSString stringWithFormat:@"Category: %@",self.gotchaItem.categoryOfItem];
        self.descriptionOfFullInfoItem.text = [NSString stringWithFormat:@"%@",self.gotchaItem.descriptionOfItem];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3 个答案:

答案 0 :(得分:1)

错误告诉您正在nameOfItem上调用UITableViewCell方法。仅这一点就足够了,但你也有堆栈跟踪,跟踪它应该不难。

在附加的代码中,这非常可疑:

cell.textLabel.text = ((Item *)[[self.itemsTable listOfItemsInCategory:sectionTitle] objectAtIndex:index]).nameOfItem;

在该行上设置一个断点,并确保它是Item而不是UITableViewCell

答案 1 :(得分:0)

您的应用正在以下行中崩溃

 self.nameOfFullInfoItem.text = [NSString stringWithFormat:@"Name: %@",self.gotchaItem.nameOfItem];

这里self.gotchaItem似乎是一个UITableViewCell。这就是为什么你不能从这个对象调用nameOfItem。确保您发送的是正确的item对象,self.gotchaItem应该是Item类的对象。

答案 2 :(得分:0)

起初我会同意其他答案,但你是说你的对象是一个项目,所以它让我想到这个答案:

您是否检查过此插座@property (weak, nonatomic) IBOutlet UILabel *nameOfFullInfoItem;是否正确连接到UILabel?进入您的界面构建器并验证只有此标签连接到此插座而没有其他内容。