2010-04-28 63 views
1

我有一个数据表,显示联系人列表。当我开始选择联系人后,所有的数据被加载correctly.But的应用,我有时会得到这个异常: -- [UITableViewRowData isEqualToString:]:无法识别的选择发送到实例0x391dce0

Program received signal: “EXC_BAD_ACCESS”. 有时

-[UITableViewRowData isEqualToString:]: unrecognized selector sent to instance 0x391dce0

最可能就是因为这个代码: -

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

} 
ExpenseTrackerAppDelegate *appDelegate = (ExpenseTrackerAppDelegate *)[[UIApplication sharedApplication] delegate]; 

Person *person = (Person *)[appDelegate.expensivePersonsList objectAtIndex:indexPath.row];

NSString *name = [NSString stringWithFormat:@"%@ %@" ,person.lastName , person.firstName];

cell.textLabel.text = name; return cell; }

如果我更换这行代码

NSString *name = [NSString stringWithFormat:@"%@ %@" ,person.lastName , person.firstName]; cell.textLabel.text = name;

与此代码 cell.textLabel.text = person.lastName;

然后一切工作正常?

我不知道究竟发生了什么? 经过查看和调试后,我发现这段代码似乎不起作用。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]]; 
PersonnalDetails *detailViewController = [[PersonnalDetails alloc] initWithNibName:@"PersonnalDetails" bundle:[NSBundle mainBundle]]; 
//detailViewController.view = [[UITableView alloc] initWithNibName:@"PersonnalDetails" bundle:[NSBundle mainBundle]]; 
// ... 
// Pass the selected object to the new view controller. 
    ExpenseTrackerAppDelegate *appDelegate = (ExpenseTrackerAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSInteger row = indexPath.row; 
Person *person = [[appDelegate expensivePersonsList] objectAtIndex:row]; 
NSLog(@"%@", person.firstName); 
    detailViewController.selectedPerson = person; 
[self.navigationController pushViewController:detailViewController animated:YES]; 
[detailViewController release]; 

} 这一行 NSLog(@"%@", person.firstName); 抛出EXC_BAD_ACESS。 我调试了代码,列表appDelegate expensivePersonsList包含对象。有人可以告诉我可能的原因吗?

+0

我想我找到了问题,问题是人的对象,它就会以某种方式释放。但是,怎么会发生这种情况,我将这个对象传递给视图控制器链。 @property (nonatomic, retain) IBOutlet Person *selectedPerson; 而我不会释放这个对象的任何视图控制器。对于EXC_BAD_ACCESS, – awsome 2010-04-28 23:13:03

回答

0

最后我发现了这个问题。实际上,我想要做的是将Person对象的所有属性设置为只读,以便只有在init方法中,属性才会被初始化。因此Person.h如下所示:

@interface Person : NSObject { 
     NSInteger pid; 
     NSString const *firstName; 
     NSString const *lastName; 
     NSString const *phoneNumber; 
     NSString const *emailAddress; 
} 

@property (readonly) NSString *firstName; 
@property (readonly) NSString *lastName; 
@property (readonly) NSString *phoneNumber; 
@property (readonly) NSString *emailAddress; 
@property (readonly) NSInteger pid;  

-(id)initWithName:(NSString *)n lastName:(NSString *)l phoneNumber:(NSString *)p emailAddress:(NSString *)e pid:(NSInteger)i; 
@end 

并且所有这些值都没有保留这些值。

这个问题是我的所有错误的主要问题: Initializing a readonly property

0

EXC_BAD_ACCESS上的调用堆栈是什么?

另外,你可以使用NSString得到一个格式化字符串,没有必要使用的NSMutableString或将其丢...

+0

,控制台上没有堆栈跟踪。它只是退出说:“编程接收到的信号:”EXC_BAD_ACCESS“ 以及关于演员和NSMutableString你是对的,我可以删除它们,但删除它们无助于解决问题。 – awsome 2010-04-28 21:18:06

1

我想你想提出一个象征性的断点“objc_exception_throw”所以你可以看到事故发生前会发生什么。只需进入Run菜单 - >管理断点 - >添加符号断点,然后输入objc_exception_throw。

顺便说一句,你可能正在调用一个已经被删除的对象。

相关问题