ios - tableView 对象获取其他对象的属性 - 如何解决它?

标签 ios objective-c object tableview addressbook

首先说明:我有一个普通的 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/

相关文章:

android - 使用 angularjs 和 cordova 推送通知插件恢复时路由更改的问题

objective-c - "_object"在 Objective-C 中意味着什么

javascript - 如何从 JS 字符串创建对象属性

java - 对象未填充

ios - RPScreenRecorder 在奇怪的情况下不录制麦克风

ios - 如何在 swift 中使用 FieldValue.serverTimestamp() 将时间戳分配给 FireStore

ios - 如何使用 Assets 库框架将图像保存在与ipad上的照片文件夹不同的文件夹中

objective-c - iPhone 应用程序的 iPad 字体大小增加

ios - Objective C 项目中的 Swift 框架

javascript - 我想返回一个对象,但我得到一个字符串