首先说明:我有一个普通的 tableView,其中包含联系人及其号码。他们可以将他们的联系人添加到应用程序中。有时他们有姓氏,有时他们没有。 问题是,当我添加只有名字的人时,其他联系人的姓氏也会被删除。当我添加具有名字和姓氏的人时,只有名字的人会得到一个 (null) 应该是姓氏的地方。 我希望我能理解它(英语不是我的母语)——我尝试使用 if 语句(正如您将在代码中看到的那样),但这并没有奏效。我还尝试在另一个数组中添加带有 firstName 的那些。 也没有用,因为你不能(我认为!)用多个数组填充一个 tableView。
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName = (__bridge_transfer 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:[[Contacts alloc] initWithFirstName: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 {
Contacts *user = [menuArray objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:@"%@", [user firstName]];
cell.textLabel.text = cellValue;
}
NSString *numbers = [thenumbers objectAtIndex:indexPath.row];
cell.detailTextLabel.text = numbers;
return cell;
}
最佳答案
您的 tableview:cellforRowAtIndexPath: 方法可以在滚动期间多次调用(事实上,每次您将新行滚动到 View 中或重新加载 TableView 的数据时,它都会被调用)。然而,在此方法中,您使用的是在添加人员时设置的“lastName”实例变量。这意味着 TableView 中的每一行都假定属于该行的联系人具有该姓氏。
代替
if (lastName && ([lastName length] > 0)) {
Contacts *user = [menuArray objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]];
cell.textLabel.text = cellValue;
} else {
Contacts *user = [menuArray objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:@"%@", [user firstName]];
cell.textLabel.text = cellValue;
}
你会想要使用
Contacts *user = [menuArray objectAtIndex:indexPath.row];
if ([user lastName] && ([[user lastName] length] > 0)) {
NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]];
cell.textLabel.text = cellValue;
} else {
NSString *cellValue = [NSString stringWithFormat:@"%@", [user firstName]];
cell.textLabel.text = cellValue;
}
关于ios - tableView 对象获取其他对象的属性 - 如何解决它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9114012/