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

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

大家好,又见面了,我是全栈君。

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

效果图:

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

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

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

 

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

 

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

复制代码
 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,如需转载请自行联系原作者

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108245.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • cpu overclocking_cpu memory overclocking

    cpu overclocking_cpu memory overclockingWhydoeskworkerhogyourCPU?TofindoutwhyakworkeriswastingyourCPU,youcancreateCPUbacktraces:watchyourprocessorload(withtoporsomething)andinmomentsofhighloadthroughkworker,executeechol>/proc/sysrq-triggertocreatea

    2025年12月6日
    4
  • 如何自动打开浏览器

    如何自动打开浏览器

    2021年9月17日
    110
  • 【SpringBoot】41、SpringBoot中使用脚本命令启动、停止程序「建议收藏」

    【SpringBoot】41、SpringBoot中使用脚本命令启动、停止程序「建议收藏」我们经常部署SpringBoot应用,一般将应用打包成jar包的方式上传至服务器,通过命令启动程序,我们每次都需要去手动敲命令来控制程序的启停,容易出错,我们可以通过脚本的方式,记住一些常用的命令1、后端启动nohupjava-jartest-1.0.jar>nohup.out2>&1&启动后,并将日志输出到nohup.out文件中2、修改配置启动nohupjava-jartest-1.0.jar–server.port=8081

    2025年9月27日
    2
  • JQuery中的select下拉框[通俗易懂]

    JQuery中的select下拉框[通俗易懂]<select id="SelectData"><optionvalue="1">dataOne</option&

    2022年7月2日
    26
  • C++读写txt文件(基本操作1)

    C++读写txt文件(基本操作1)本博客主要写了用C++读写txt本文的基本操作,最简单的写入和读出两个基本操作。本程序的功能是向Test.txt文件文件写入字符串”ThisisaTest12!”和读取字符串”ThisisaTest12!”,并且将读取到的字符串存到temp变量(char型变量),且输出到控制台窗口进行显示。注意:1.1当创建ofstream对象后,可以像操作cout一样操作这个对象,…

    2022年5月5日
    303
  • 搭建LAMP架构_redis搭建集群

    搭建LAMP架构_redis搭建集群搭建LAMP架构一、LAMP架构概述1、LAMP简介LAMP架构是目前成熟的企业网站应用模式之一,指的是协同工作的一整台系统和相关软件,能够提供动态web站点服务及其应用开发环境。LAMP是一个缩写词,具体包括Linux操作系统,Apache网站服务器,MySQL数据库服务器,PHP(或perl,Python)网页编程语言。2、LAMP构成组件(1)Linux系统:LAMP架构的基础,提供用于支撑Web站点的操作系统(2)Apache网站服务:LAMP架构的前端,向用户提供网站服务、发送网

    2022年10月10日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号