简体   繁体   中英

tableView objects get property of other objects - how to work around it?

First of all the explanation: I have a normal tableView with contacts and their number in it. They can add their contacts there to the app. Sometimes they have a last name, sometimes they dont. Thing is, when I add someone with only a first name, the last names of the other contacts get deleted too. And when I add someone with first and last name, the ones with only first name get a (null) where the last name is supposed to be. I hope I made it understanding (english is not my native tongue) - I tried it with if statements (as you will see in the code), but that didnt do the trick. I also tried in adding the ones with firstName in another array. Also didnt work, because you can't (I think!) populate a tableView with more than one array.

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

In your scenario it is better to use ABRecordCopyCompositeName that returns :

Returns an appropriate, human-friendly name for the record. For person records: The concatenated value of these properties: Prefix, Suffix, Organization, First name, and Last name.

Look at it here - ABRecordCopyCompositeName in ABRecord reference

Your tableview:cellforRowAtIndexPath: method can be called multiple times during scrolling (in fact it gets called every time you scroll a new row into view, or when you reload the table view's data). Yet in this method you work with the 'lastName' instance variable that was set when you added a person. This means that every row in your table view assumes that the contact belonging to that row has that one last name.

Instead of

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;
}

You will want to use

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM