【发布时间】:2016-01-28 06:54:20
【问题描述】:
我有一个数组,用于存储手机中的所有联系人。我有一个 UITableView,我想在其中显示所有联系人。我在表格视图中显示它们时遇到问题 o 我创建了一个新数组,并使用下面的代码循环遍历联系人数组并从中获取字符串并将它们存储在新数组中 (contactsNew)
for (CNContact *contact in contacts)
{
NSString *string = [formatter stringFromContact:contact];
[contactsNew addObject:string];
NSLog(@"contact = %@", string);
}
问题是所有联系人都显示在日志中,但我仍然无法在表格视图中显示联系人。
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [contactsNew count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row];
return cell;
}
我检查了 'string' 变量的值以及添加到 contactsNew 数组中的数量和内容,一切正常。
我也可以用其他东西代替:
cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row];
这样我就可以直接使用联系人数组,而不是创建一个新数组。提前致谢。
【问题讨论】:
-
在for循环后尝试[tableView reloadData]
-
@Tejas 请在我的答案中出现一些延迟后重新加载 tableview。
标签: ios objective-c uitableview cncontact cncontactstore