热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

IOSUITableView解析(一)

UITableView的作用由于Iphone的大小有限,所以UITableView的作用是巨大的。比如QQ,微博等应用都用到了该控件。UITableVi
UITableView的作用

由于Iphone的大小有限,所以UITableView的作用是巨大的。

比如QQ,微博等应用都用到了该控件。

UITableView可以在有限的控件内,显示无限的数据。

而UITableview中的每一行都是一个UITableViewCell对象,那么不管我们要在每一行显示什么数据,只要重新定制UITableViewCell即可。

 

UITableView的数据源UITableViewDataSource

UITableView是MVC中的View,所以自然不负责存储表中的数据,而是负责如何显示数据的。

那么就要设置UITableView的数据源,UITableViewDataSource是一个协议,该协议里边定义了一些接口,

包括UITableView具有几个section,每个section里面有几行。以及得到显示在某一行的UITableViewCell对象。

具体的做法是:controller实现UITableViewDataSource协议,并实现该协议里边的方法。

协议里边的方法分为required和optional

@required//必须的//得到某个section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;//得到显示要显示在某个section中某一行的UITableViewCell对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;@optional//可选的- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented,如果没有实现该方法,则默认是1个section

具体的实现如下:其中dataModel是Model类的实例,该类提供了访问自身数据的接口。

ViewController.h

1 #import
2 #import "Model.h"
3 @interface ViewController : UIViewController
4
5 @property UITableView* myTableView;
6 @property Model *dataModel;
7 @end

 

ViewController.m

1 #import "ViewController.h"
2
3 @interface ViewController ()
4
5 @end
6
7 @implementation ViewController
8 @synthesize myTableView;
9 @synthesize dataModel;
10 - (void)viewDidLoad {
11 [super viewDidLoad];
12 //dataModel对象是MVC中的Model
13 dataModel = [[Model alloc]initModel];
14 myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
15 //设置dataSource,
16 myTableView.dataSource = self;
17 [self.view addSubview:myTableView];
18
19 }
20
21 - (void)didReceiveMemoryWarning {
22 [super didReceiveMemoryWarning];
23 // Dispose of any resources that can be recreated.
24 }
25
26 //UITableView在显示数据的时候调用该方法,得到有几行
27 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
28 return [dataModel getCount];
29 }
30
31 //NNSIndexPath在UITableView中的作用是用来得到section和row
32 -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
33 //cell的键
34 static NSString * cellIdentifier = @"cellIdentifier";
35 //队里中保存了键为cellIdentifier的cell对象, 这样做的目的是为了复用对象资源
36 //避免视图滚动时不断地创建和释放资源。
37 UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
38 //队列中可能没有闲置的cell对象,所以需要检查一下。
39 if(cell==nil){
40 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
41 }
42
43
44 cell.textLabel.text = [dataModel objectInDataArrayAtIndex:indexPath.row];
45 return cell;
46 }
47 @end

 

Model.h

1 #import
2
3 @interface Model : UIView
4 @property NSArray *dataArray;
5
6 -(instancetype)initModel;
7 -(NSInteger)getCount;
8 -(id)objectInDataArrayAtIndex:(NSUInteger)index;
9 @end

 

Model.m

1 #import "Model.h"
2
3 @implementation Model
4 @synthesize dataArray;
5
6 -(instancetype)initModel{
7 self = [super init];
8 if(self){
9 //实际上应该从数据库读取文件的,
10 dataArray = [NSArray arrayWithObjects:@"first",@"second",@"third",nil];
11 }
12 return self;
13 }
14 -(NSInteger)getCount{
15 return [dataArray count];
16 }
17 -(id)objectInDataArrayAtIndex:(NSUInteger)index{
18 return dataArray[index];
19 }
20 @end

 

在UITableView中显示图片

前面知道了UITableViewCell负责UITableView中的每一行显示什么,所以只要对UITableViewCell对象做些工作即可。

只要设置cell中的imageView中的image就能在UITableView中显示图片。

需要注意的是只能设置image属性而不能直接设置imageView属性,因为该属性是只读的。

1 NSString *imageNamed = [NSString stringWithString:@"gerbera.png"];
2 cell.imageView.image = [UIImage imageNamed:imageNamed];

这样,就显示图片了

 

转:https://www.cnblogs.com/beMaster/p/4982381.html



推荐阅读
  • Startup 类配置服务和应用的请求管道。Startup类ASP.NETCore应用使用 Startup 类,按照约定命名为 Startup。 Startup 类:可选择性地包括 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • ASP.NET MVC中Area机制的实现与优化
    本文探讨了在ASP.NET MVC框架中,如何通过Area机制有效地组织和管理大规模应用程序的不同功能模块。通过合理的文件夹结构和命名规则,开发人员可以更高效地管理和扩展项目。 ... [详细]
  • 本文将深入探讨如何在不依赖第三方库的情况下,使用 React 处理表单输入和验证。我们将介绍一种高效且灵活的方法,涵盖表单提交、输入验证及错误处理等关键功能。 ... [详细]
  • 本文介绍了如何使用JavaScript的Fetch API与Express服务器进行交互,涵盖了GET、POST、PUT和DELETE请求的实现,并展示了如何处理JSON响应。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 本文探讨了在使用Azure Active Directory进行用户身份验证时,结合AddAuthentication和RequireAuthenticatedUser的必要性及其潜在冗余问题。 ... [详细]
  • Struts与Spring框架的集成指南
    本文详细介绍了如何将Struts和Spring两个流行的Java Web开发框架进行整合,涵盖从环境配置到代码实现的具体步骤。 ... [详细]
  • 在项目部署后,Node.js 进程可能会遇到不可预见的错误并崩溃。为了及时通知开发人员进行问题排查,我们可以利用 nodemailer 插件来发送邮件提醒。本文将详细介绍如何配置和使用 nodemailer 实现这一功能。 ... [详细]
  • 中科院学位论文排版指南
    随着毕业季的到来,许多即将毕业的学生开始撰写学位论文。本文介绍了使用LaTeX排版学位论文的方法,特别是针对中国科学院大学研究生学位论文撰写规范指导意见的最新要求。LaTeX以其精确的控制和美观的排版效果成为许多学者的首选。 ... [详细]
author-avatar
DTor惜涵_237
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有