iOS开发:用户数据与表格视图处理全攻略
立即解锁
发布时间: 2025-08-25 01:24:33 阅读量: 1 订阅数: 6 

### iOS开发:用户数据与表格视图处理全攻略
在iOS开发中,处理用户数据和使用UITableView是非常重要的技能。下面将详细介绍如何设置联系人信息、查看联系人以及创建和定制UITableView。
#### 1. 设置联系人信息
设置联系人信息有两种方法,下面为你详细介绍具体步骤。
##### 1.1 手动创建联系人
- **创建项目**:使用Single View Application模板创建一个名为“Chapter8Recipe8”的新项目,类前缀为“Main”。
- **添加框架**:将AddressBook.framework和AddressBookUI.framework添加到项目中。
- **设置界面**:在视图控制器的XIB文件中添加一个UIButton,并将其连接到名为`-newContactPressed:`的动作。
- **配置协议和导入框架**:在头文件中导入框架,并使视图控制器符合`ABNewPersonViewControllerDelegate`协议。
```objc
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
```
- **实现方法**:定义两个方法,一个用于处理按钮点击,另一个作为`ABNewPersonViewController`的委托方法。
```objc
-(void)newContactPressed:(id)sender
{
ABNewPersonViewController *view = [[ABNewPersonViewController alloc] init];
view.newPersonViewDelegate = self;
UINavigationController *newNavigationController = [[UINavigationController alloc]
initWithRootViewController:view];
[self presentModalViewController:newNavigationController animated:YES];
}
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonView
didCompleteWithNewPerson:(ABRecordRef)person
{
if (person == NULL)
{
NSLog(@"User Cancelled Creation");
}
else
NSLog(@"Successfully Created New Person");
[self dismissModalViewControllerAnimated:YES];
}
```
##### 1.2 编程方式创建联系人
- **修改界面**:在界面中添加一系列UITextFields,并为每个字段设置占位符文本,描述其用途。
- **连接属性**:将每个UITextField连接到头文件中的相应属性。
```objc
@property (strong, nonatomic) IBOutlet UITextField *textFieldFirst;
@property (strong, nonatomic) IBOutlet UITextField *textFieldLast;
@property (strong, nonatomic) IBOutlet UITextField *textFieldPhone;
@property (strong, nonatomic) IBOutlet UITextField *textFieldStreet;
@property (strong, nonatomic) IBOutlet UITextField *textFieldCity;
@property (strong, nonatomic) IBOutlet UITextField *textFieldState;
@property (strong, nonatomic) IBOutlet UITextField *textFieldZip;
```
- **实现代码**:在`-newContactPressed:`方法中添加以下代码来创建新联系人。
```objc
ABMutableMultiValueRef multi =
ABMultiValueCreateMutable(kABMultiStringPropertyType);
CFErrorRef anError = NULL;
ABMultiValueIdentifier multivalueIdentifier;
bool didAdd, didSet;
didAdd = ABMultiValueAddValueAndLabel(multi, (__bridge
CFStringRef)self.textFieldPhone.text,
kABPersonPhoneMobileLabel,
&multivalueIdentifier);
if (!didAdd)
{
NSLog(@"Error Adding Phone Number");
}
ABRecordRef aRecord = ABPersonCreate();
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, (__bridge
CFStringRef)self.textFieldFirst.text, nil);
ABRecordSetValue(aRecord, kABPersonLastNameProperty, (__bridge
CFStringRef)self.textFieldLast.text, nil);
didSet = ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
if (!didSet)
{
NSLog(@"Error Setting Phone Value");
}
CFRelease(multi);
ABMutableMultiValueRef address =
ABMultiValueCreateMutable(kABDictionaryPropertyType);
// Set up keys and values for the dictionary.
CFStringRef keys[5];
CFStringRef values[5];
keys[0] = kABPersonAddressStreetKey;
keys[1] = kABPersonAddressCityKey;
keys[2] = kABPersonAddressStateKey;
keys[3] = kABPersonAddressZIPKey;
keys[4] = kABPersonAddressCountryKey;
values[0] = (__bridge CFStringRef)self.textFieldStreet.text;
values[1] = (__bridge CFStringRef)self.textFieldCity.text;
values[2] = (__bridge CFStringRef)self.textFieldState.text;
values[3] = (__bridge CFStringRef)self.textFieldZip.text;
values[4] = CFSTR("USA");
CFDictionaryRef aDict = CFDictionaryCreate(kCFAllocatorDefault,(void *)keys,
(void *)values,5,
&kCFCopyStringDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
ABMultiValueIdentifier dictionaryIdentifier;
bool didAddAddress;
didAddAddress = ABMultiValueAddValueAndLabel(address, aDict, kABHomeLabel,
&dictionaryIdentifier);
if (!didAddAddress)
{
NSLog(@"Error Adding Address");
}
CFRelease(aDict);
ABRecordSetValue(aRecord, kABPersonAddressProperty, address, nil);
```
- **选择实现方式**:有两种方式实现新联系人的创建。
- **使用`ABNewPersonViewController`**:设置`view.displayedPerson = aRecord;`,让用户确认联系人信息。
- **直接编程创建**:使用以下代码直接保存联系人。
```objc
ABAddressBookRef addressBook = ABAddressBookCreate();
ABAddressBookAddRecord(addressBook, aRecord, nil);
ABAddressBookSave(addressBook, nil);
```
- **释放变量**:在方法结束前释放变量。
```objc
CFRelease(aRecord);
CFRelease(address);
```
#### 2. 查看联系人
查看联系人的步骤如下:
- **创建项目**:创建一个名为“Chapter8Recipe9”的新项目,类前缀为“MainViewController”。
- **导入框架和添加表格视图**:导入Address Book和Address Book UI框架,并在视图控制器中添加一个UITableView,将其连接到头文件中的`tableViewContacts`属性。
- **配置协议和属性**:使视图控制器符合`UITableViewDelegate`、`UITableViewDataSource`和`ABPersonViewControllerDelegate`协议,并创建一个`contacts`属性来存储联系人列表。
```objc
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface MainViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, ABPersonViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableViewContacts;
@property (strong, nonatomic) NSArray *contacts;
@end
```
- **设置导航控制器**:在应用委托的实现文件中设置导航控制器。
```objc
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Overrid
```
0
0
复制全文
相关推荐










