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方法进行创建,否则,不创建。

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!


本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4793052.html,如需转载请自行联系原作者
目录
相关文章
|
6月前
|
Web App开发 iOS开发 开发者
ios证书类型及其作用说明
ios证书类型及其作用说明
52 0
|
人工智能 文字识别 API
iOS MachineLearning 系列(4)—— 静态图像分析之物体识别与分类
本系列的前几篇文件,详细了介绍了Vision框架中关于静态图片区域识别的内容。本篇文章,我们将着重介绍静态图片中物体的识别与分类。物体识别和分类也是Machine Learning领域重要的应用。通过大量的图片数据进行训练后,模型可以轻易的分析出图片的属性以及图片中物体的属性。
236 0
|
算法 API iOS开发
iOS MachineLearning 系列(3)—— 静态图像分析之区域识别
本系列的前一篇文章介绍了如何使用iOS中自带的API对图片中的矩形区域进行分析。在图像静态分析方面,矩形区域分析是非常基础的部分。API还提供了更多面向应用的分析能力,如文本区域分析,条形码二维码的分析,人脸区域分析,人体分析等。本篇文章主要介绍这些分析API的应用。
217 0
|
7月前
|
程序员 开发工具 iOS开发
iOS 获取手机的型号,系统版本,软件名称,软件版本,手机类型(型号)
iOS 获取手机的型号,系统版本,软件名称,软件版本,手机类型(型号)
82 0
|
7月前
|
Android开发 iOS开发
判断手机端获取哪种类型的手机系统-安卓-ios
判断手机端获取哪种类型的手机系统-安卓-ios
|
9月前
|
存储 缓存 iOS开发
iOS 轻量化动态图像下载缓存框架实现
日常开发过程中,图片的下载会占用大量的带宽,图片的加载会消耗大量的性能和内存,正确的使用图片显得尤为重要。 同样也经常需要在各类型控件上读取网络图片和处理本地图片,例如:UIImageView、UIBtton、NSImageView、NSButton等等。
iOS 轻量化动态图像下载缓存框架实现
|
iOS开发
IOS的UITableView控件简单使用
IOS的UITableView控件简单使用
124 0
|
人工智能 API iOS开发
iOS MachineLearning 系列(2)—— 静态图像分析之矩形识别
本系列文章将完整的介绍iOS中Machine Learning相关技术的应用。本篇文章开始,我们将先介绍一些与Machine Learning相关的API的应用。使用这些API可以快速方便的实现很多如图像识别,分析等复杂功能,且不会增加应用安装包的体积。
223 0
|
缓存 算法 测试技术
iOS UITableView性能优化
iOS UITableView性能优化
iOS UITableView性能优化
|
iOS开发
iOS开发-导航栏标题动画
iOS开发-导航栏标题动画
160 0
iOS开发-导航栏标题动画