iOS:带主标题、副标题、图像类型的表格视图UITableView

  制作一个通讯录,包括姓名、电话、头像,将表格视图类型设置为UITableViewCellStyleSubtitle

效果图:

//创建一个联系人的类,初始化数据

 

 

  在视图控制器中实现表格内容的显示

 1 #import "ViewController.h"
 2 #import "Contact.h"
 3 #define NUM 20
 4 
 5 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
 6 @property (weak, nonatomic) IBOutlet UITableView *tableView;
 7 @property (strong,nonatomic)NSMutableArray *contacts; //联系人数组
 8 @end
 9 
10 @implementation ViewController
11 
12 - (void)viewDidLoad
13 {
14     [super viewDidLoad];
15     //初始化
16     for(int i=0; i<NUM; i++)
17     {
18         Contact *contact = [[Contact alloc]initWithContactName:[NSString stringWithFormat:@"name%d",i] andTelPhoneNumber:[NSString stringWithFormat:@"tel:1876645%04d",arc4random_uniform(NUM)]];
19         [self.contacts addObject:contact];
20     }
21     
22     //设置数据源和代理
23     self.tableView.dataSource = self;
24     self.tableView.delegate = self;
25 }
26 
27 #pragma mark -tableView的数据源方法
28 //每一个section有多少行
29 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
30 {
31     return self.contacts.count;
32 }
33 //设置每一个单元格的内容
34 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
35 {
36     //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
37     static NSString *reuseIdentifier = @"contactCell";
38     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
39     //2.如果没有找到,自己创建单元格对象
40     if(cell == nil)
41     {
42         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
43     }
44     
45     //3.设置单元格对象的内容
46     
47     //设置图像
48     [cell.imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform(9)]]];
49     //设置主标题
50     cell.textLabel.text = [self.contacts[indexPath.row] contactName];
51     //设置副标题
52     cell.detailTextLabel.text = [self.contacts[indexPath.row] telphoneNumner];
53     
54     
55     //设置字体颜色
56     cell.textLabel.textColor = [UIColor orangeColor];
57     cell.detailTextLabel.textColor = [UIColor blueColor];
58     
59     return cell;
60 }
61 
62 #pragma mark -tableView的代理方法
63 //设置行高
64 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
65 {
66     return 70;
67 }
68 
69 //懒加载(重写get方法)
70 -(NSMutableArray*)contacts
71 {
72     if(!_contacts)
73     {
74         _contacts =  [NSMutableArray arrayWithCapacity:NUM];
75     }
76     return _contacts;
77 }
78 @end

  所谓懒加载,就是当该对象做为具有特性的@property属性时,只有在需要该对象的时候,通过重写它的get方法进行创建,否则,不创建。

posted @ 2015-09-08 22:00  XYQ全哥  阅读(783)  评论(0编辑  收藏  举报