【问题标题】:How to add contact string from one array to another如何将联系人字符串从一个数组添加到另一个数组
【发布时间】: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


【解决方案1】:

创建数组后需要重新加载tableView,所以在for循环后使用[tableView reloadData]

for (CNContact *contact in contacts)
{
    NSString *string = [formatter stringFromContact:contact];
    [contactsNew addObject:string];
    NSLog(@"contact = %@", string);
}

[tableview reloadData]; //Add this line

希望这会奏效。

【讨论】:

    【解决方案2】:

    您需要在延迟一段时间后重新加载 tableview。 像这样使用

     for (CNContact *contact in contacts)
    {
        NSString *string = [formatter stringFromContact:contact];
        [contactsNew addObject:string];
        NSLog(@"contact = %@", string);
    }
    
    //  add after Delay 0.5 if its not worked then 1.0
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tableview reloadData];
    });
    

    【讨论】:

      【解决方案3】:
      Try this
      
      for (CNContact *contact in contacts)
      {
              NSString *string = [formatter stringFromContact:contact];
              [contactsNew addObject:string];
              NSLog(@"contact = %@", string);
      }
      [tableview reloadData];
      

      【讨论】:

        【解决方案4】:

        获取联系人后,您必须在主线程上重新加载表格...例如:- dispatch_async(dispatch_get_main_queue(), ^{ [_table reloadData]; });

        ...为你的表格视图写

        cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row]valueForKey:@"keys";

        ...在此处提供密钥。

        【讨论】:

          【解决方案5】:

          您需要检查以下几点才能解决您的问题:

          • 您需要检查的第一件事是在将值添加到新数组后调用一次的天气表视图方法?如果没有,请在向新数组添加值后尝试重新加载表视图。
          • 如果在向新数组添加值后调用正确,请尝试在cellForRowAtIndexPath 方法中使用NSLog

          希望对你有所帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-08-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-13
            • 2012-06-22
            • 1970-01-01
            • 2022-01-14
            相关资源
            最近更新 更多