我有一个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;
}
是的,我认为你是对的。只是再试一次,我可以添加任何人只有名字和它的作品或人名和姓。如果我同时尝试,我会在提到的行中获得SIGABRT。任何想法我可以做些什么= – Blade 2012-01-30 14:15:30
感谢您的编辑。所以我应该每次添加一个联系人时清除menuArray? – Blade 2012-01-30 17:39:02
您应该在'... shouldContinueAfterSelectingPerson'中重建它之前清除menuArray。目前你每次只追加到现有的数组中,但是你改变了在整个对象中使用的'lastName'。从上面的代码中不清楚你的目标是使用'lastName'还是'menuArray'。 – 2012-01-30 18:06:03