【问题标题】:How to display fetch result on table view如何在表格视图上显示获取结果
【发布时间】:2016-01-07 07:52:24
【问题描述】:

我想显示获取的数据现在显示在 tableview 结帐我的代码

- (void) fetchContacts
{

    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            //keys with fetching properties
            NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
            if (error) {
                NSLog(@"error fetching contacts %@", error);
            } else {
                NSString *phone;
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                UIImage *profileImage;
                NSMutableArray *contactNumbersArray;
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;
                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }
                    UIImage *image = [UIImage imageWithData:contact.imageData];
                    if (image != nil) {
                        profileImage = image;
                    }else{
                        profileImage = [UIImage imageNamed:@"person-icon.png"];
                    }
                    for (CNLabeledValue *label in contact.phoneNumbers) {
                        phone = [label.value stringValue];
                        if ([phone length] > 0) {
                            [contactNumbersArray addObject:phone];
                        }
                    }
                    NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
                    [_Contacts  addObject:personDict];

                    NSLog(@"%@",phone);
                    NSLog(@"%@",fullName);
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                 [self.contacttableview reloadData];



                });
            }
        }
    }];

}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.Contacts count];



}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    cell = [_contacttableview dequeueReusableCellWithIdentifier:@"cell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];







        UIImage *image = [UIImage imageNamed:@" person-icon.png"];
        cell.imageView.image = image;

    }


   cell.textLabel.text = [_Contacts objectAtIndex:indexPath.row];

    return cell;
}

从此人的代号和电话号码进入控制台,但无法在 tableview 上显示。

现在我想代替文本标签的人名来了,在详细标签文本中它的电话号码来了

在单元格图像中,它的联系人图片将来自他存储在他的 iPhone 上的任何内容

【问题讨论】:

  • 你是否像这样初始化_Contacts:_Contacts = [NSMutableArray array];

标签: ios xcode uitableview nsdictionary contacts


【解决方案1】:

在获取时,您已将 NSDictionary 对象存储在 self.contacts 数组中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    cell = [_contacttableview dequeueReusableCellWithIdentifier:@"cell"];

    //get person dictionary first    
    NSDictionary* personDict = [_Contacts objectAtIndex:indexPath.row];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

   //get person image from dictionary and assign to cell.image.
   UIImage *image = [UIImage imageNamed:[personDict objectForKey@"userImage"]];
   cell.imageView.image = image;

   //get person name from dictionary and assign to cell.fullname.
   cell.textLabel.text = [personDict objectForKey@"fullName"];

   //There is no extra label on default UITableview cell, so you can add your description as bellow with same text label or you can use CustomCell
    cell.textLabel.text = [NSString stringWithFormat:@"%@\n%@",[personDict objectForKey@"fullName"],[personDict objectForKey@"PhoneNumbers"]];

    return cell;
}  

注意:如果您不想在一个默认的 UITableviewcell 文本标签中显示全名和电话号码,请准备自定义单元格。

【讨论】:

  • 我为它写代码的行方法是对是错
  • 是的。您的行方法代码是正确的。在 [_Contacts objectAtIndex:indexPath.row] 上,您将直接获得 NSDictionary 对象而不是一个 fulsome。您可以在 _contacts 数组中添加 NSDictionary 对象的地方签入获取联系人功能代码 - [_Contacts addObject:personDict];........ 从 NSDictionary 对象中,您将获得全名、电话号码和 userImage。
  • 你能用自定义单元格说明吗
  • 是的,我已经在我的项目中尝试过这个例子,但是当编译器来到那一行时 if(cell == nil){ NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self选项:无];单元格 = [笔尖 objectAtIndex:0]; } 它给出异常 n 信号 sigbart 并开始崩溃
【解决方案2】:

在您的表格视图单元格中使用以下代码ForRowAtIndexPath,

NSDictionary* personDict = [_Contacts objectAtIndex:indexPath.row];
cell.textLabel.text = [personDict objectForKey:@"fullName"]
cell.detailTextLabel.text = [personDict objectForKey:@"PhoneNumbers"]
cell.imageView.image = userImage;

【讨论】:

    【解决方案3】:

    如果未初始化,则初始化联系人数组。

    正如我所见,您正在将字典添加到数组中。从数组中,您可以获取如下对象:

    cell.textLabel.text = [[self.Contacts objectAtIndex:indexPath.row]objectForKey:@"fullName"];
    
    cell.imageView.image = [[self.Contacts objectAtIndex:indexPath.row]objectForKey:@"userImage"];
    

    【讨论】:

      【解决方案4】:

      您是否使用自定义单元格 r 普通单元格。 使用自定义单元格你会得到

      并检查 json 格式尝试使用这个 cutsom 单元格

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * cellIdentifier=@"Cell";

      UITableViewCell * cell=[self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];
      if (cell==nil) {
          cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
      }
      
      UITextField * text=(UITextField *)[cell viewWithTag:1];
      
      NSLog(@"%@",text);
      
      return  cell;
      

      }

      【讨论】:

      • srry 我使用的是普通单元格,这就是为什么
      • 你能用自定义单元格说明吗
      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2015-01-13
      • 2019-09-21
      相关资源
      最近更新 更多