使用Xcode 4.3中的地址簿中的联系人填充tableview

时间:2012-03-21 20:41:57

标签: iphone ipad xcode4.3

我正在为iPad和iPhone制作应用程序。在第一个屏幕中你需要登录,这是有效的。但后来我来到下一个屏幕女巫是一个tableview。我希望这个视图是iPad或iPhone上的地址簿上的名字(名字和姓氏)。

我得到了tableview,但它是空的。有人能帮助我吗?

这是我的控制器(.m,我不需要编辑.h):

#import "TableViewController.h"
#import "ViewController.h"
#import "AddressBook/AddressBook.h"

@interface TableViewController ()
@end

@implementation TableViewController
{
NSMutableArray *ListOfItems;
}

@synthesize tblContacts;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[self ListContacts];
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[self setTblContacts:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:   (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)Back:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}

- (void)ListContacts{
/*ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

for ( int i = 0; i < nPeople; i++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
    UITableView ContactList 
} */

ListOfItems = [[NSMutableArray alloc] init];

[ListOfItems addObject:@"Iceland"];
[ListOfItems addObject:@"Greenland"];
[ListOfItems addObject:@"Switzerland"];
[ListOfItems addObject:@"Norway"];
[ListOfItems addObject:@"New Zealand"];
[ListOfItems addObject:@"Greece"];
[ListOfItems addObject:@"Rome"];
[ListOfItems addObject:@"Ireland"];

self.navigationItem.title = @"Countries";
}

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section         {
return [ListOfItems count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero   reuseIdentifier:CellIdentifier];
}

// Set up the cell...
NSString *cellValue = [ListOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;

return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

@end

请注意,我试图用一些硬编码值填充我的表格。但是,我的桌子一直空着。请帮忙。

5 个答案:

答案 0 :(得分:0)

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
@interface AddressBookViewController: UIViewController <ABPeoplePickerNavigationControllerDelegate> {}
-(IBAction) btnClicked:(id)sender;
@end

这是从您自己的应用程序访问联系人应用程序的AddressBook API。也许你可以使用它,它比实现你自己的UITableView和所有它快得多。

希望它有所帮助。

答案 1 :(得分:0)

您是否设置了表的委托和数据源?您可以通过Interface Builder轻松完成:

  1. 在Interface Builder中选择表格视图。
  2. 转到右侧栏的连接器检查器(最后一个选项卡,您可以在其中链接奥特莱斯和操作)。
  3. 将数据源和代理出口链接到文件所有者(或具有管理表格视图的方法的信息的文件)。
  4. 请记住在文件的所有者身份检查器中指定您自己的类(再次,在Xcode的右侧栏中,第三个选项卡)。

    希望这能帮到你!

答案 2 :(得分:0)

cell.textLabel.text = [listOfItems objectAtIndex:indexpath.row];

并确保将属性中的标识符设置为Cell。

答案 3 :(得分:0)

这是一年之后,但希望这会对某人有所帮助:

您似乎在ListContacts方法下面注释了一些非常重要的代码。

删除{ - 1}}下的/**/ - 您的代码应该至少返回一些值。

答案 4 :(得分:0)

尝试使用[self.tableView reloadData];在对象中添加数据后