Uitableview 串联时NSString具有空值
我有一个包含多个部分的UITableView。tableview的一部分有两行,其中一行是可编辑的(插入按钮),另一行显示addressbook中的名称。 单击单元格中的“插入”按钮,我将加载peoplePickerView并选择一个联系人 我从通讯录中得到的联系人是Uitableview 串联时NSString具有空值,uitableview,ios5,nsstring,abpeoplepickerview,Uitableview,Ios5,Nsstring,Abpeoplepickerview,我有一个包含多个部分的UITableView。tableview的一部分有两行,其中一行是可编辑的(插入按钮),另一行显示addressbook中的名称。 单击单元格中的“插入”按钮,我将加载peoplePickerView并选择一个联系人 我从通讯录中得到的联系人是 - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSel
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *middleName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
self.contactName = [NSString stringWithFormat:@"%@/%@/%@", firstName ?: @"", middleName ?: @"", lastName ?: @""];
[self.myTableView reloadData];
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
在tableview的CellForRowatineXpath上
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0){
if(indexPath.row == 0){
cell.textLabel.text = self.contactName;
NSLog(@"Contact Name %@", self.contactName);
}
else{
cell.textLabel.text = @"";
}
}
}
当我设置为字符串时,只有firstname属性,那么字符串就有正确的值,但是当我尝试连接字符串(first+middle+last names)并重新加载tableview时,我会得到一个空值。我做错了什么,如何纠正?尝试替换下面的行
self.contactName = [NSString stringWithFormat:@"%@/%@/%@", firstName ?: @"", middleName ?: @"", lastName ?: @""];
像这样检查
self.contactName = [[NSString alloc] initWithFormat:@"%@/%@/%@", firstName ?: @"", middleName ?: @"", lastName ?: @""];
你必须确保两件事
[NSString stringWithFormat…
之前,您正在初始化self.contactName,例如在viewDidLoad中,请执行以下self.contactName=[NSString alloc]init]
2.如果尝试将nil字符串连接到字符串,最终结果也将为nil,请确保连接代码中包含的所有字符串都有值,而不是nil我犯的错误是将属性contactName声明为弱
@property(nonatomic, weak) NSString *contactName;
按照Ahmad的建议,在连接字符串之前还要检查null。我犯的错误是将属性contactName声明为弱
@property(非原子,弱)NSString*contactName代码>@XaviValero-这很好,如果需要,也可以尝试上面的代码。