如何在表视图上显示获取结果

时间:2016-01-07 07:52:24

标签: ios xcode uitableview nsdictionary contacts

我想显示现在显示的数据显示在tableview checkout我的代码

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

从这个代码名称和人的电话号码进入控制台,但无法在桌面视图上显示。

现在我要在文字标签名称的位置来详细标签文本中其电话号码来了

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

4 个答案:

答案 0 :(得分:0)

如果未初始化,则初始化Contacts数组。

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

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

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

答案 1 :(得分:0)

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

- (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文本标签中显示Fullname和Phonenumber,请准备自定义单元格。

答案 2 :(得分:0)

您使用自定义细胞r正常细胞吗? 使用自定义单元格你会得到

并检查json格式尝试使用此cutom单元格

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

}

答案 3 :(得分:0)

在tableview cellForRowAtIndexPath中使用以下代码,

 private void PopulateListBox(ref ComboBox cmb, string Folder, string FileType)
    {
        DirectoryInfo dinfo = new DirectoryInfo(Folder);
        FileInfo[] Files = dinfo.GetFiles(FileType);
        foreach (FileInfo file in Files)
        {
            var ext = Path.GetExtension(file.Name);
            var name = Path.GetFileNameWithoutExtension(file.Name);
            cmb.Items.Add(name);
        }
    }