手機通訊錄實現

來源:互聯網
上載者:User

標籤:

<pre name="code" class="objc">首先重寫UITableViewCell初始化方法:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        // Initialization code        self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 80, 80)];        _photoView.layer.cornerRadius = 40;        _photoView.layer.masksToBounds = YES;        [self.contentView addSubview:_photoView];        [_photoView release];                self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 60, 35)];        [self.contentView addSubview:_nameLabel];        [_nameLabel release];                self.ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 5, 40, 35)];        [self.contentView addSubview:_ageLabel];        [_ageLabel release];                self.genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(260, 5, 40, 35)];        [self.contentView addSubview:_genderLabel];        [_genderLabel release];                self.phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 50, 200, 35)];        [self.contentView addSubview:_phoneNumberLabel];        [_phoneNumberLabel release];    }    return self;}

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>@property (nonatomic, retain) NSDictionary *dic;@property (nonatomic, retain) NSArray *titles;@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        self.dic =  [self readDataFromPlist];        self.titles = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)];    }    return self;}- (NSDictionary *)readDataFromPlist{    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"AddressBook-2" ofType:@"plist"];    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];    return dic;}- (void)loadView{    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;    tableView.separatorColor = [UIColor lightGrayColor];    tableView.dataSource = self;    tableView.delegate = self;    tableView.rowHeight = 90;    self.view = tableView;    [tableView release];}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationItem.title = @"全部連絡人";}#pragma mark - UITableViewDataSource//設定行數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.dic[self.titles[section]] count];}//建立cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *identifier = @"mark";    StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (!cell) {        cell = [[[StudentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];    }    NSDictionary *dic = self.dic[self.titles [indexPath.section]][indexPath.row];    cell.photoView.image = [UIImage imageNamed:[dic objectForKey:@"imageName"]];    cell.nameLabel.text = [self.dic[self.titles [indexPath.section]][indexPath.row] objectForKey:@"name"];    cell.ageLabel.text = [self.dic[self.titles [indexPath.section]][indexPath.row] objectForKey:@"age"];    cell.genderLabel.text = [self.dic[self.titles [indexPath.section]][indexPath.row] objectForKey:@"gender"];    cell.phoneNumberLabel.text = [self.dic[self.titles [indexPath.section]][indexPath.row] objectForKey:@"phoneNumber"];    return cell;}//設定分區- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return [self.titles count];}//頁首- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return self.titles[section];}//索引值- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    return self.titles;}#pragma mark - UITableViewDelegate//當cell被選中時觸發- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *str = [self.dic[self.titles [indexPath.section]][indexPath.row] objectForKey:@"phoneNumber"];    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",str]]];    //2、用UIWebView來實現。打電話結束後會返回當前應用程式:    UIWebView *callPhoneWebVw = [[UIWebView alloc] init];    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tell://%@",str]]];    [callPhoneWebVw loadRequest:request];}
通過上述步驟,連絡方式可以很簡單.

著作權聲明:本文博主原創文章。部落格,未經同意不得轉載。

手機通訊錄實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.