OGeek|极客世界-中国程序员成长平台

标题: iOS - "unrecognised selector sent to instance"错误,当名称标签 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:56
标题: iOS - "unrecognised selector sent to instance"错误,当名称标签

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

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

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell nameOfItem]: unrecognized selector sent to instance 0x7f8e7ab5ed40'

我的对象:

@interface Item : NSObject

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

-(instancetype)initItemNSString *)name categoryNSString *)category descriptionNSString *)description;
-(NSComparisonResult)compareItem *)otherItem;

@end

我的手机:

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;

}

我的行选择:

-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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)prepareForSegueUIStoryboardSegue *)segue senderid)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

异常出现在行,这是我的第二个 View :

#import "InfoViewController.h"

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

@end



Best Answer-推荐答案


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

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

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

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

关于iOS - "unrecognised selector sent to instance"错误,当名称标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34360109/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4