2012-01-30 51 views
1

我有一个tableView,类似于Contacts。用户可以添加他的联系人并获取姓名和显示的号码。当联系人只有名字而不是姓氏时,他显示名字和(空)。为了避免它,我使用了一个if语句。但由于某种原因,我总是在cell.textLabel.text = value;处发生SIGABRT错误。如何在一个对象为零而另一个对象不是时显示数据?

下面是代码以获得数据:

- (BOOL)peoplePickerNavigationController: 

(ABPeoplePickerNavigationController *)peoplePicker 

    shouldContinueAfterSelectingPerson:(ABRecordRef)person { 

    firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 

    ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty); 
    number = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0); 
    [thenumbers addObject:number]; 

if(lastName && ([lastName length] > 0)) { 
[menuArray addObject:[[Contacts alloc] initWithFirstName:firstName andLastName:lastName]]; 
    } else { 
     [menuArray addObject:firstName];} 


    [self dismissModalViewControllerAnimated:YES]; 
    return NO; 
    } 

而这里的代码以将其显示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];} 
if(lastName && ([lastName length] > 0)) { 
    Contacts *user = [menuArray objectAtIndex:indexPath.row]; 
    NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]]; 
    cell.textLabel.text = cellValue; 
} else { 
    NSString *value = [menuArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = value; 

} 

NSString *numbers = [thenumbers objectAtIndex:indexPath.row]; 
cell.detailTextLabel.text = numbers; 

return cell; 
} 

回答

2

SIGABRT装置抛出异常。在控制台日志中查找实际的异常。我怀疑你实际上得到它上一行:

NSString *value = [menuArray objectAtIndex:indexPath.row]; 

最可能的错误是indexPath.row是过去的menuArray末。


编辑:我想我看到你的错误。它实际上可能是“不响应选择器”错误。当您构建自己的menuArray时,有时需要添加Contacts,有时需要添加NSString。 (Contacts是这个对象的一个​​非常奇怪的名字;它似乎是一个人)。你永远不会清除旧的menuArray,我怀疑当你改变lastName时,你正在清理那些陈旧的信息,以后发生冲突

另请注意,您正在泄漏lastNamefirstName。您需要__bridge_transfer这里,而不是__bridge

+0

是的,我认为你是对的。只是再试一次,我可以添加任何人只有名字和它的作品或人名和姓。如果我同时尝试,我会在提到的行中获得SIGABRT。任何想法我可以做些什么= – Blade 2012-01-30 14:15:30

+0

感谢您的编辑。所以我应该每次添加一个联系人时清除menuArray? – Blade 2012-01-30 17:39:02

+0

您应该在'... shouldContinueAfterSelectingPerson'中重建它之前清除menuArray。目前你每次只追加到现有的数组中,但是你改变了在整个对象中使用的'lastName'。从上面的代码中不清楚你的目标是使用'lastName'还是'menuArray'。 – 2012-01-30 18:06:03

2

这是相当不友好的混合对象类型在同一个数组中。这可能是值得检查的对象是什么,你觉得是这样,那么,你说:

Contacts *user = [menuArray objectAtIndex:indexPath.row]; 
NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]]; 

试着说:

Contacts *user = [menuArray objectAtIndex:indexPath.row]; 
if ([user isKindOfClass:[Contacts class]]) 
{ 
    NSLog(@"Okay, it's fine"); 
} 
else 
{ 
    NSLog(@"Oops, I messed up"); 
} 

我的直觉是,也许有时它是一个字符串,当你期待它是一个联系人对象。

+0

嘿它返回“好吧,这很好” – Blade 2012-01-30 17:04:18

+0

我同意尼克混合类型一个阵列让我......紧张......我认为你的实际问题是你没有清除menuArray,但首选的解决方案是重写'Contacts'来处理名字(更好的是,只需将'Contacts'作为'ABRecordRef')。 – 2012-01-30 17:36:48

+0

谢谢,但我不知道该怎么做。联系人是视图控制器btw。是的,我以前做过,只处理firstName,但有时候人们想要那里的姓氏,所以我想让这个选项打开:) – Blade 2012-01-30 18:25:59

相关问题