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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • native2ascii java_Native2Ascii和Ascii2Native的Java实现

    native2ascii java_Native2Ascii和Ascii2Native的Java实现packageutil;/***native2ascii.exeJavacodeimplementation.**@author*@version1.0*/publicclassNative2AsciiUtils{/***prefixofasciistringofnativecharacter*/privatestaticStringPREFIX=”\…

    2025年8月31日
    3
  • arping指令linux,arping

    arping指令linux,arping例a,指定IP发送ARP请求[root@Blackghost~]arping192.168.1.11ARPING192.168.1.11from192.168.1.6eth0Unicastreplyfrom192.168.1.11[08:00:27:7e:b8:08]2.780msUnicastreplyfrom192.168.1.11[08:00:27:7e:b8…

    2022年6月10日
    43
  • Kali Linux三种网络攻击方法总结(DDoS、CC和ARP欺骗)

    Kali Linux三种网络攻击方法总结(DDoS、CC和ARP欺骗)本文章使用的是KaliLinux的2020-4-installer-amd64版本KaliLinux的安装过程本文章不做过多说明,请自行百度一、DDos攻击首先,打开一个命令行输入以下命令:gitclonehttps://github.com/Ha3MrX/DDos-Attack提示如图所示这样,用于DDos的数据包就已经下载到了你的Kali上下面,进入你所下载的DDos文件夹,输入命令(注意大小写):cdDDos-Attack然后设置ddos-attack.py设置

    2022年7月11日
    131
  • html中的导航条制作「建议收藏」

    html中的导航条制作「建议收藏」在网页中一个这样的导航条该怎么做呢?用HTML中的无序列表(ul)做然后在给列表设置需要的样式即可:具体参考代码:&amp;amp;lt;!DOCTYPEhtml&amp;amp;gt;&amp;amp;lt;htmllang=&amp;quot;en&amp;quot;&amp;amp;gt;&amp;amp;lt;head&amp;amp;gt; &amp;amp;lt;metachar

    2022年7月22日
    20
  • 计算机二级Python

    计算机二级Python概述计算机二级在近两年新加了python的选择,趁机考了一下,顺便记录一下学习的一些所获第一章程序设计语言概述考纲考点:这一部分主要是介绍计算机语言的公共常识,一些尝试我就按照自己的理解方式

    2022年7月6日
    18
  • WPF日期时间控件

    WPF日期时间控件最近一个WPF项目需要用到日期时间控制,因为WPF自带的控件只有日期没办法选择时间,所以后面用到了一个DateTimePicker控件,支持日期和时间的选择,但使用过程发现有一些小bug,所以进行修正。控制的效果如下:…

    2022年5月13日
    36

发表回复

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

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