iOS - 名称标签时出现 "unrecognised selector sent to instance"错误

标签 ios objective-c

我正在发送一些对象,当我将表格中的单元格按到另一个 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)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;

}

我的行选择:

-(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

异常出现在行,那是在我的第二个 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

最佳答案

错误告诉您正在调用 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/

相关文章:

ios - 在 View 上自定义多个手势

iOS——区分 NSDictionary 和 NSMutableDictionary?

ios - 混合 iOS 网络和 native 应用程序事件的通信

ios - 如何使用 Core Graphics/Quartz 2D 在 iOS 中制作圆锥形渐变?

objective-c - 如何用三角形弹出覆盖来呈现 UIView?

objective-c - NSRunAlertPanel 是否在任何空间上显示警报? - Objective-C/10.7+

ios - SLRequest - Twitter 返回代码 215(错误的身份验证数据)

objective-c - 如何找到一条直线上的点 - Objective c?

ios - 错误 - 链接器命令失败,退出代码为 1

ios - 将隐藏数据设置为 uitableview 单元格