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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM