• 手机通讯录实现


    <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];
    }
    
    通过上述步骤,联系方式可以很简单.

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    数据结构与算法(3-4)--矩阵的压缩存储
    数据结构与算法(3-3)--队列的应用
    数据结构与算法(3-2)--栈的应用
    数据结构与算法(3-1)--栈和队列
    数据结构与算法(2)--线性表(数组和链表)
    数据结构与算法(1)--时间及空间复杂度
    python变量与地址的关系
    python高级(03)--socket编程
    python高级(02)--生成器和迭代器
    python处理http接口请求
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4832173.html
Copyright © 2020-2023  润新知