我正在创建一个应用程序,您可以按一个按钮打开您的联系人列表。然后,您可以选择要添加的联系人,并将其名称和电子邮件导入应用程序。我目前有这些信息进入标签,但我想将其添加到表格视图单元格。我该怎么做?
我的代码:
·H:
#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
@interface FirstViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>
- (IBAction)showPicker:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *firstName;
@property (weak, nonatomic) IBOutlet UILabel *email;
@end
的.m:
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize firstName;
@synthesize email;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showPicker:(id)sender {
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
}
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
self.firstName.text = name;
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *emailId = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);//0 for "Home Email" and 1 for "Work Email".
self.email.text = emailId;
}
@end
答案 0 :(得分:1)
好的,我将解释如何以编程方式实现一个非常基本的表视图控制器。但是,您可以自行决定如何将其集成到您的应用程序中。
让我们从头文件开始,我们称之为MyTableViewController.h
:
@interface MyTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
}
@end
如您所见,您的控制器类采用协议UITableViewDelegate
和UITableViewDataSource
。
现在让我们看一下实现文件MyTableViewController.m
中的第一个片段。显然,你的第一份工作是创建控制器的视图。您可以在控制器的loadView
方法中执行此操作。如果您想了解有关视图生命周期以及如何编写UIViewController
的详细信息,建议您阅读UIViewController class reference和随附的View Controller Programming Guide。
- (void) loadView
{
// Give the view some more or less arbitrary initial size. It will be
// resized later when it is actually displayed
CGRect tableViewFrame = CGRectMake(0, 0, 320, 200);
UITableView* tableView = [[[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStyleGrouped] autorelease];
self.view = tableView;
// Here we make sure that the table view will take as much horizontal
// and vertical space as it can get when it is resized.
UIViewAutoresizing autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
tableView.autoresizingMask = autoresizingMask;
// We need to tell the table view that we are both its delegate and
// its data source.
tableView.delegate = self;
tableView.dataSource = self;
}
只是为了让您知道:如果您的控制器是loadView
的子类,您可以完全省略UITableViewController
,但我故意不采用该快捷方式,以便我可以向您展示表视图的需求代表和数据源。最重要的是数据源。
在MyTableViewController.m
的下一个代码段中,我们将实现一些基本的UITableViewDataSource
方法。为此,您需要了解表视图的结构:表视图分为几个部分,每个部分都有多个单元格。具有部分的要点是在视觉上分离单元格组,具有可选的部分页眉或页脚。但是,我不会在这里详细说明这一点。
- (NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
// Let's keep it simple: We want just one section
return 1;
}
- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
// Let's keep it simple: We want just one row, or table view cell.
// Since we only have one section (see above) we don't have to look
// at the section parameter.
return 1;
}
现在,最后,您创建表格视图单元格的核心。同样,这是我们实现的UITableViewDataSource
方法。请注意,我们不需要仅检查indexPath
参数,因为我们知道我们只有一个部分和一行。在实际应用程序中,您可能必须编写检查indexPath.section
和indexPath.row
的switch-case或if-else语句,以便区分您需要创建的不同单元格。
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// This is very important for your future table view implementations:
// Always ask the table view first if it already has a cell in its
// cache. If you don't do this your table view will become slow when
// it has many cells.
NSString* identifier = @"MyTableViewCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
// Aha, the table view didn't have a cell in its cache, so we must
// create a new one. We use UITableViewCellStyleValue1 so that the
// cell can display two pieces of information.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
}
// Regardless of whether we got the cell from the table view's cache
// or create a new cell, we must now fill it with content.
// First, obtain the information about the person from somewhere...
NSString* personName = ...;
NSString* personEmail = ...;
// ... then add the information to the table cell
cell.textLabel.text = personName;
cell.detailTextLabel.text = personEmail;
return cell;
}
最后,我们实施了UITableViewDelegate
方法:
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Here you can react to the user tapping on the cell. If you
// don't want the user to be able to select a cell you can
// add the following line to tableView:cellForRowAtIndexPath:
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
很难说你应该如何将它集成到你的应用程序中。这一切都取决于您想要显示表格视图的位置。既然您说要替换已有的两个标签,可能的方法之一就是:
FirstViewController
FirstViewController
添加插座FirstViewController
采用协议UITableViewDelegate
和UITableViewDataSource
FirstViewController
连接到表格视图的委托和数据源出口loadView
,您不需要它,您已经在Interface Builder中建立了所有连接等如果您需要有关集成的进一步帮助,我建议您提出一个新问题,并可能参考此答案。祝你好运。