gpt4 book ai didi

objective-c - 无法识别的选择器发送到实例 - UITableView 和 Restkit LoadObjects 选项

转载 作者:行者123 更新时间:2023-11-28 23:01:57 25 4
gpt4 key购买 nike

我在 restkit google 组中问过这个问题,但现在意识到它实际上可能是一个更广泛的问题,而不是特定于 RK 的问题。

我已经在这个问题上兜圈子几个小时了。我正在尝试使用从 Web 服务检索并使用 RestKits 对象映射存储在对象中的数据来填充 UITableView。调用服务、存储对象都非常完美。

问题是当我尝试为表重新加载数据时。我收到这个错误 -

'NSInvalidArgumentException', reason: '-[CoursesModel text]: unrecognized selector sent to instance and then app shuts down and 

takes me here:
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([RoundShoutAppDelegate class]));
}
Thread 1: signal SIGABRT

CoursesModel 是我设置的模型。如果我没有将 tableview 的委托(delegate)指定为类(我知道我必须这样做),那么类(class)将在 table view 中完美加载。但是,当我尝试单击其中一个单元格(在 didSelectRowAtIndexPath 处)时,我会收到错误消息。因此请关注事实上,我必须指定一个代表,我知道有些事情真的搞砸了,我无法弄清楚。

我假设有些东西在不应该发布的时候发布了,但数据在 _courses 数组中。顺便说一下,这是一个 ARC 项目。

这是一些相关的代码,但如果您需要更多,请告诉我。如果有人有任何信息可以指出我正确的方向,那就太棒了!感谢您的帮助!

MainViewController.h 
@interface MainViewController : UITableViewController <CLLocationManagerDelegate, RKObjectLoaderDelegate, UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *coursesTableView;
NSArray *_courses;
}

@end

类(class)模型.h

@interface CoursesModel : NSObject { 
NSNumber* _courseID;
NSString* _courseName;
NSString* _city;
NSString* _state;
NSNumber* _distance;
}

@property (nonatomic, retain) NSNumber* courseID;
@property (nonatomic, retain) NSString* courseName;
@property (nonatomic, retain) NSString* city;
@property (nonatomic, retain) NSString* state;
@property (nonatomic, retain) NSNumber* distance;
@end

主视图 Controller .m

//Most of the code ommited for your sanity 
// All good up to this point
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:
(NSArray*)objects {
//NSLog(@"Loaded courses %@", objects);
_courses = objects;
// There is data in _courses.
NSLog(@"Loaded courses in array %@", _courses);
[coursesTableView reloadData];

委托(delegate)方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGSize size = [[[_courses objectAtIndex:indexPath.row] text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 9000)]; 
return size.height + 10;
}

最佳答案

我现在看到了!您的委托(delegate)方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

CGSize size = [[[_courses objectAtIndex:indexPath.row] text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 9000)];

return size.height + 10;

}

错了。 NSArray 没有名为 -text 的方法,因此它导致将无法识别的选择器发送到 NSArray。为什么编译器没有就此警告您?如果您需要字符串,请使用 -stringValue

至于您的委托(delegate),这现在完全有意义,因为不设置委托(delegate)不会在重新加载时调用此方法。

关于objective-c - 无法识别的选择器发送到实例 - UITableView 和 Restkit LoadObjects 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9848046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com