我是xcode的新手,我有这个问题,应用程序崩溃给我这个错误,当我尝试点击一个单元格转到DetailViewcontroller时出错: * 由于未捕获的异常而终止应用程序' NSInvalidArgumentException',原因:' - [__ NSCFDictionary练习]:无法识别的选择器发送到实例0x8ca15b0'。我不确定是什么原因。我在这里复制了代码:
MAsterViewcontroller.h
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController{
NSMutableArray* routines_;
NSMutableArray *routineinfo_;
}
@property (nonatomic,retain)NSMutableArray* routines;
@property (nonatomic,retain)NSMutableArray* routineinfo;
@end
文件MasterViewController.m
#import "MasterViewController.h"
#import "ClassDetailViewController.h"
#import "ClassConstants.h"
@implementation MasterViewController
@synthesize routines=routines_,routineinfo=routineinfo_;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Mills" ofType:@"plist"];
routines_= [[NSMutableArray alloc] initWithContentsOfFile:path];
//Uncomment the following line to displa an Edit button in the navgation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void)dealloc {
[routines_ release];
[routineinfo_ release];
[super dealloc];
}
- (void)insertNewObject:(id)sender
{
// if (!_objects) {
// _objects = [[NSMutableArray alloc] init];
// }
// [_objects insertObject:[NSDate date] atIndex:0];
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.routines count];
}
//Configure appearence of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//Configure the cell.
cell.textLabel.text = [[self.routines objectAtIndex:indexPath.row]
objectForKey:NAME_KEY];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
// Get the data given the selected row
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger IndexSelected=[indexPath row];
//NSDictionary *routinedata = [self->routines_ objectAtIndex:[indexPath row]];
// Pass that data on to the DetailViewController
DetailViewController *detailVC = segue.destinationViewController;
NSString *pathSelected=@"";;
switch (IndexSelected)
{
case 0:
pathSelected = [[NSBundle mainBundle] pathForResource:@"Mills0" ofType:@"plist"];
break;
case 1:
pathSelected = [[NSBundle mainBundle] pathForResource:@"Mills1" ofType:@"plist"];
break;
case 2:
pathSelected = [[NSBundle mainBundle] pathForResource:@"Mills2" ofType:@"plist"];
break;
default:
break;
}
routineinfo_= [[NSMutableArray alloc] initWithContentsOfFile:pathSelected];
detailVC.routineinfo = self.routineinfo;
}
}
@end
#import <UIKit/UIKit.h>
@interface DetailViewController : UITableViewController{
@private
NSMutableArray *routineinfo_;
}
@property (nonatomic, retain) NSMutableArray *routineinfo;
@end
ClassDetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UITableViewController{
@private
NSMutableArray *routineinfo_;
}
@property (nonatomic, retain) NSMutableArray *routineinfo;
@end
ClassDetailViewController.m文件
#import "ClassDetailViewController.h"
#import "ClassConstants.h"
#import "MillsDetailsConstant.h"
#import "LesMillsCell.h"
@implementation DetailViewController
@synthesize routineinfo=routineinfo_;
- (void) dealloc {
[super dealloc];
[routineinfo_ release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.routineinfo count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"LesMillsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
LesMillsCell *LesMillsCell=self.routineinfo [indexPath.row];
//Configure the cell EXERCISE
LesMillsCell.Exercise.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:EXERCISE_KEY];
//Configure the cell SONG
LesMillsCell.Title.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:SONG_KEY];
//Configure the cell ARTIST
LesMillsCell.Artist.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:ARTIST_KEY];
//Configure the cell TIME
LesMillsCell.Artist.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:TIME_KEY];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//Configure appearence of table view cells.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//setto la sezione
// NSInteger nSection;
// setto le righe per sezione
// NSInteger nRows;
// nRows= [self.tableView numberOfRowsInSection:1];
// creo una cella prptotypes per ogni riga della sezione
// NSIndexPath IndexTableView
// nRows=self.tableView
}
@end
子类文件LesMillsCell.h
#import <UIKit/UIKit.h>
@interface LesMillsCell : UITableViewCell{
@private
// UITableViewCell *lesmillscell_;
UILabel *Exercise_;
UILabel *Title_;
UILabel *Artist_;
UILabel *Time_;
NSMutableArray *routineinfo_;
}
@property (nonatomic, retain) IBOutlet UILabel *Exercise;
@property (nonatomic, retain) IBOutlet UILabel *Title;
@property (nonatomic, retain) IBOutlet UILabel *Artist;
@property (nonatomic, retain) IBOutlet UILabel *Time;
@end
LesMillsCells.m
#import "LesMillsCell.h"
#import "ClassConstants.h"
#import "MillsDetailsConstant.h"
@implementation LesMillsCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
谢谢你的帮助!
答案 0 :(得分:1)
如果崩溃发生在这一行:
LesMillsCell.Exercise.text= [[self.routineinfo objectAtIndex:indexPath.row] objectForKey:EXERCISE_KEY];
这意味着LesMillsCell不是LesMillsCell类的对象,而是NSDictionary(或__NSCFDictionary)。因此,对该对象使用Exercise将导致错误,因为字典不支持该方法。错误从这里开始:
LesMillsCell *LesMillsCell=self.routineinfo [indexPath.row];
self.routineinfo [indexPath.row]返回id类型的对象,因此编译器无法知道实际上是什么类型,因此赋值没有警告。
此外,您以两种不同的方式使用routineinfo。首先,您将其视为LesMillsCell
LesMillsCell *LesMillsCell=self.routineinfo [indexPath.row];
下面的一行将您视为字典,并提取EXERCISE_KEY的值:
[[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:EXERCISE_KEY];