草庐IT

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

coder 2024-01-23 原文

我正在发送一些对象,当我将表格中的单元格按到另一个 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 - 名称标签时出现 "unrecognised selector sent to instance"错误的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  4. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  5. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  6. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  7. ruby - 在院子里用@param 标签警告 - 2

    我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?

  8. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  9. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  10. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

随机推荐