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