基于UITableView写的简单通讯录

来源:互联网 发布:愿你知我心 mp3 下载 编辑:程序博客网 时间:2024/04/28 03:57

主要代码

点击打开链接可以在这下载写的工程代码

#import "RootViewController.h"#import "Student.h"@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        [selfreadDataFromPlist];        self.titles = [[self.dicallKeys] sortedArrayUsingSelector:@selector(compare:)];            }    return self;}- (void)readDataFromPlist{    NSString *filePath = [[NSBundlemainBundle] pathForResource:@"AddressBook1"ofType:@"plist"];    self.dic = [NSDictionarydictionaryWithContentsOfFile:filePath];}- (void)loadView{    UITableView *tableView = [[UITableViewalloc] initWithFrame:[UIScreenmainScreen].bounds style:UITableViewStylePlain];    tableView.separatorColor = [UIColororangeColor];    //指定tableView的数据源(为tableView显示数据支持)需要服从UItableViewDateSouce协议    tableView.dataSource = self;    //设置tableView的代理(用来处理cell的滴啊你事件)    tableView.delegate = self;    //设置行高    tableView.rowHeight = 80;    self.view = tableView;    [tableView release];}- (void)viewDidLoad{    [superviewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColorgrayColor];    //设置标题    self.navigationItem.title =@"河南9班通讯录";}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.dic[self.titles[section]]count];}//用来创建cell,每一行都i要对应一个cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    Student *cell = [[Studentalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];    cell.photoView.image= [UIImageimageNamed:self.dic[self.titles[indexPath.section]][indexPath.row][@"imageName"]];        cell.nameLabel.text =self.dic[self.titles[indexPath.section]][indexPath.row][@"name"];    cell.ageLabel.text =self.dic[self.titles[indexPath.section]][indexPath.row][@"age"];    cell.genderLabel.text =self.dic[self.titles[indexPath.section]][indexPath.row][@"gender"];    cell.numberLabel.text =self.dic[self.titles[indexPath.section]][indexPath.row][@"phoneNumber"];        return [cell autorelease];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return [self.diccount];}//设置每个分区的显示的文字(页眉)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return self.titles[section];}//设置tableView右边的索引值(用来快速查找- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    return self.titles;}- (void)didReceiveMemoryWarning{    [superdidReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/- (void)dealloc{    self.dic = nil;    self.titles = nil;    [super dealloc];}@end


0 0
原创粉丝点击