iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

一、项目文件结构和plist文件

 

二、实现效果

三、代码示例

1.没有使用配套的类,而是直接使用xib文件控件tag值操作

数据模型部分:

YYtg.h文件

复制代码

//
// YYtg.h
// 01-团购数据显示(没有配套的类)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Global.h"

@interface YYtg : NSObject
@property(nonatomic,copy)NSString *buyCount;
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *price;
@property(nonatomic,copy)NSString *title;
YYinitH(tg)
@end

 
复制代码

YYtg.m文件

复制代码

//
// YYtg.m
// 01-团购数据显示(没有配套的类)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYtg.h"

@implementation YYtg
YYinitM(tg)
@end

 
复制代码

主控制器

YYViewController.m文件

复制代码

//
// YYViewController.m
// 01-团购数据显示(没有配套的类)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYtg.h"

@interface YYViewController ()<UITableViewDataSource>
@property(nonatomic,strong)NSArray *tg;
@property (strong, nonatomic) IBOutlet UITableView *tableview;

@end

@implementation YYViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.tableview.rowHeight=100;

}

#pragma mark- 懒加载
-(NSArray *)tg
{
if (_tg==nil) {
NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];

NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];
for (NSDictionary *dict in temparray) {
YYtg *tg=[YYtg tgWithDict:dict];
[arrayM addObject:tg];
}
_tg=[arrayM mutableCopy];
}
return _tg;
}

#pragma mark-数据显示
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tg.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//读取xib中的数据
// NSArray *arrayM=[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil];
// UITableViewCell *cell=[arrayM firstObject];
static NSString *identifier=@"tg";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
// cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell= [[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil] firstObject];
}

YYtg *tg=self.tg[indexPath.row];
//设置数据
//使用tag
UIImageView *imgv=(UIImageView *)[cell viewWithTag:1];
imgv.image=[UIImage imageNamed:tg.icon];
UILabel *buyCount=(UILabel *)[cell viewWithTag:4];
buyCount.text=[NSString stringWithFormat:@"已有%@人购买",tg.buyCount];
UILabel *title=(UILabel *)[cell viewWithTag:2];
title.text=tg.title;
UILabel *price=(UILabel *)[cell viewWithTag:3];
price.text=[NSString stringWithFormat:@"$%@",tg.price];


//返回cell
return cell;
}

//隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end

复制代码

使用xib自定义的UItableviewcell

代码分析:

上面的代码通过使用xib文件中各个控件的tag值,完成对每个部分数据的赋值和刷新。但是,作为主控制器,它应该知道xib文件中各个控件的tag值,它知道的是不是太多了呢?

为了解决上面的问题,我们可以为自定义的cell设置一个配套的类,让这个类来操作这个xib,对外提供接口,至于内部的数据处理,外界不需要关心,也不用关心。

改造后的代码如下:

 

2.使用xib和对应的类完成自定义cell的数据展示

新建一个类,用来管理对应的xib文件

注意类的继承类,并把该类和xib文件进行关联

YYtgcell.h文件代码:

复制代码

//
// YYtgcell.h
// 02-团购(使用xib和类完成数据展示)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "YYtg.h"

@interface YYtgcell : UITableViewCell
@property(nonatomic,strong)YYtg *yytg;

@end

复制代码

 YYtgcell.m文件

复制代码

//
// YYtgcell.m
// 02-团购(使用xib和类完成数据展示)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYtgcell.h"
//私有扩展
@interface YYtgcell()
@property (strong, nonatomic) IBOutlet UIImageView *img;
@property (strong, nonatomic) IBOutlet UILabel *titlelab;
@property (strong, nonatomic) IBOutlet UILabel *pricelab;
@property (strong, nonatomic) IBOutlet UILabel *buycountlab;
@end
@implementation YYtgcell

#pragma mark 重写set方法,完成数据的赋值操作
-(void)setYytg:(YYtg *)yytg
{
_yytg=yytg;
self.img.image=[UIImage imageNamed:yytg.icon];
self.titlelab.text=yytg.title;
self.pricelab.text=[NSString stringWithFormat:@"$%@",yytg.price];
self.buycountlab.text=[NSString stringWithFormat:@"已有%@人购买",yytg.buyCount];
}
@end

 
复制代码

主控制器

YYViewController.m文件

复制代码

//
// YYViewController.m
// 02-团购(使用xib和类完成数据展示)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYtg.h"
#import "YYtgcell.h"

@interface YYViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;

@property(strong,nonatomic)NSArray *tg;
@end

@implementation YYViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.tableview.rowHeight=80.f;
}
#pragma mark- 懒加载
-(NSArray *)tg
{
if (_tg==nil) {
NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];

NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];
for (NSDictionary *dict in temparray) {
YYtg *tg=[YYtg tgWithDict:dict];
[arrayM addObject:tg];
}
_tg=[arrayM mutableCopy];
}
return _tg;
}


#pragma mark- xib创建cell数据处理
#pragma mark 多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tg.count;
}
#pragma mark设置每组每行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier= @"tg";
YYtgcell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
//如何让创建的cell加个戳
//对于加载的xib文件,可以到xib视图的属性选择器中进行设置
cell=[[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]firstObject];
NSLog(@"创建了一个cell");
}

//设置cell的数据
//获取当前行的模型
YYtg *tg=self.tg[indexPath.row];
cell.yytg=tg;
return cell;
}

-(BOOL)prefersStatusBarHidden
{
return YES;
}

@end

复制代码

3.对上述代码进行进一步的优化和调整(MVC)

优化如下:

(1)把主控制器中创建cell的过程抽取到YYtgcell中完成,并对外提供一个接口。

YYtgcell.h文件(提供接口)

复制代码

#import <UIKit/UIKit.h>
#import "YYtgModel.h"

@interface YYtgcell : UITableViewCell
@property(nonatomic,strong)YYtgModel *yytg;

//把加载数据(使用xib创建cell的内部细节进行封装)
+(instancetype)tgcellWithTableView:(UITableView *)tableView;
@end

 
复制代码

YYtgcell.m文件(把创建自定义cell的部分进行封装)

复制代码

//
// YYtgcell.m
// 02-团购(使用xib和类完成数据展示)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYtgcell.h"
//私有扩展
@interface YYtgcell()
@property (strong, nonatomic) IBOutlet UIImageView *img;
@property (strong, nonatomic) IBOutlet UILabel *titlelab;
@property (strong, nonatomic) IBOutlet UILabel *pricelab;
@property (strong, nonatomic) IBOutlet UILabel *buycountlab;
@end
@implementation YYtgcell

#pragma mark 重写set方法,完成数据的赋值操作
-(void)setYytg:(YYtgModel *)yytg
{
_yytg=yytg;
self.img.image=[UIImage imageNamed:yytg.icon];
self.titlelab.text=yytg.title;
self.pricelab.text=[NSString stringWithFormat:@"$%@",yytg.price];
self.buycountlab.text=[NSString stringWithFormat:@"已有%@人购买",yytg.buyCount];
}

+(instancetype)tgcellWithTableView:(UITableView *)tableView
{
static NSString *identifier= @"tg";
YYtgcell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
//如何让创建的cell加个戳
//对于加载的xib文件,可以到xib视图的属性选择器中进行设置
cell=[[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]firstObject];
NSLog(@"创建了一个cell");
}
return cell;
}

@end

 
复制代码

主控器中的业务逻辑更加清晰,YYViewController.m文件代码如下

复制代码

//
// YYViewController.m
// 02-团购(使用xib和类完成数据展示)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYtgModel.h"
#import "YYtgcell.h"

@interface YYViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;

@property(strong,nonatomic)NSArray *tg;
@end

@implementation YYViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.tableview.rowHeight=80.f;
}
#pragma mark- 懒加载
-(NSArray *)tg
{
if (_tg==nil) {
NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];

NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];
for (NSDictionary *dict in temparray) {
YYtgModel *tg=[YYtgModel tgWithDict:dict];
[arrayM addObject:tg];
}
_tg=[arrayM mutableCopy];
}
return _tg;
}


#pragma mark- xib创建cell数据处理

#pragma mark 多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

#pragma mark多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tg.count;
}

#pragma mark设置每组每行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.创建cell
YYtgcell *cell=[YYtgcell tgcellWithTableView:tableView];

//2.获取当前行的模型,设置cell的数据
YYtgModel *tg=self.tg[indexPath.row];
cell.yytg=tg;

//3.返回cell
return cell;
}

#pragma mark- 隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}

@end

复制代码

四、推荐调整的项目文件结构

这是调整后的文件结构,完整的MVC架构。

注意:注意文件的命名规范。

提示技巧:批量改名,操作如下:

 

修改为想要的名称:

 

转载于:https://www.cnblogs.com/LifeTechnologySupporter/p/9686108.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/265956.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

硬件模块化开发的例子:LD3320语音模块与Source Insight的使用

目录硬件模块化开发的行情LD3320语音模块使用手册介绍&#xff1a;语音模块验货测试硬件连接效果图测试结果良好Source Insight代码查看器创建工程修改字体配色方案中文乱码关联代码语音识别模块代码分析语音识别模块二次开发硬件模块化开发的行情 LD3320语音模块使用手册介绍&…

ubuntu 远程桌面及Qt安装

环境&#xff1a; ubunut14 x64 目标&#xff1a;通过vnc远程桌面&#xff0c;在ubuntu 上编写qt程序 1.安装远程桌面 apt-get install xrdp vnc4server xbase-clients dconf-editor #安装xrdp&#xff0c;vnc4server apt-get install gnome-panel gnome-settings-daemon m…

awk分割字符

为什么80%的码农都做不了架构师&#xff1f;>>> str1|_|2|_|3|_||_|4|_|5|_|6|_||_|7|_|8|_|9; 期望分割成 1|_|2|_|3 4|_|5|_|6 7|_|8|_|9 也就是以|_||_|做分割符&#xff0c;但是&#xff0c;字符段长度不确定&#xff0c;有可能分割成3段&#xff0c;也可能会有…

面经——嵌入式软件工程师面试遇到的经典题目

参考&#xff1a;嵌入式软件工程师面试遇到的经典题目 作者&#xff1a;一只青木呀 发布时间&#xff1a; 2020-11-04 23:43:16 网址&#xff1a;https://blog.csdn.net/weixin_45309916/article/details/109499825 目录1、找错误2、下面的代码输出是什么&#xff0c;为什么&am…

axure实现复选框全选_AxureRP8实战手册-案例73(全选与取消全选效果)

案例73. 全选与取消全选效果案例来源:百度音乐-音乐盒案例效果&#xff1a;初始状态/取消全选时&#xff1a;(图5-117)全选后取消任一选项时&#xff1a;(图5-118)全选/单选全部选中时&#xff1a;(图5-119)案例描述&#xff1a;列表中相邻的行具有交替的背景颜色&#xff1b;点…

移植Linux3.4.2版本内核到mini2440

一. 内核启动流程&#xff0c;据此配置内核(机器ID) 1. 获取内核源码 www.kernel.org https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.4.2.tar.bz2 2. 解压内核 # tar xjvf linux-3.4.2.tar.bz2 3. 修改顶层Makefile&#xff0c;指定交叉编译器 ARCH ? arm CRO…

在浏览器的背后(二) —— HTML语言的语法解析

当你看到这篇文章意味着我辜负了教主的殷切期望周末木有去约会&#xff0c;以及苏老师我思故我在北京鼓楼的落井下石成功了…… 本文demo powered by 已经结婚的老赵的不再维护的wind.js 物是人非啊…… 说回正经事&#xff0c;在上一篇文章中&#xff0c;我们取得了初步成果&a…

面经——小米面经(2021春招)

摘自&#xff1a;小米面经&#xff08;2021春招&#xff09;——感谢小米、感谢雷总、感谢上官可编程 作者&#xff1a;阿波罗啦啦啦啦 发布时间&#xff1a; 2021-05-01 11:08:41 网址&#xff1a;https://blog.csdn.net/weixin_44933419/article/details/116325554 3月31日投…

使用arm混合汇编计算两个64位的和_混合使用C、C++和汇编语之: C、C++ 和 ARM 汇编语言之间的调用...

12.4C target_blank stylecursor:pointer;color:#D05C38;text-decoration:underline;>C、C和ARM汇编语言之间的调用本节提供一些示例&#xff0c;显示如何从C调用C和汇编语言代码&#xff0c;以及从C和汇编语言调用C代码。其中包括调用约定和数据类型。主要包括下面内容&…

记一次用WPScan辅助渗透WordPress站点

记一次用WPScan辅助渗透WordPress站点 一、什么是WPScan&#xff1f; WPScan 是一个扫描 WordPress 漏洞的黑盒子扫描器&#xff0c;它可以为所有 Web 开发人员扫描 WordPress 漏洞并在他们开发前找到并解决问题。我们还使用了 Nikto &#xff0c;它是一款非常棒的 Web 服务器评…

移植tslib(s3c2440)

解压安装tslib # tar -zxvf tslib-1.4.tar.gz # cd tslib # ./autogen.sh #echo “ac_cv_func_malloc_0 _nonnullyes”>arm-linux.cache # ./configure –hostarm-linux –cache-filearm-linux.cache -prefix/usr/local/tslib ac_cv_func_malloc_0_nonnullyes # make …

什么是Cortex、ARMv8、arm架构、ARM指令集、soc

参考&#xff1a;到底什么是Cortex、ARMv8、arm架构、ARM指令集、soc&#xff1f;一文帮你梳理基础概念【科普】 发布时间&#xff1a; 一口Linux 网址&#xff1a;https://blog.csdn.net/daocaokafei/article/details/109008103 目录前言1. ARM公司2. ARM内核与架构1&#xff…

Windows下Qt5搭建Android开发环境笔记

Windows很大的特点是配置使用几乎都可以图形化进行&#xff0c;和Linux比起来在很多时候配置环境也要方便很多。所以&#xff0c;搭建Qt for Andorid也是十分简单的。需要以下工具&#xff1a;1.最方便的Qt官方包&#xff0c;现在还处于RC阶段&#xff0c;经过测试也是有些小bu…

大学python怎么过_大学生该不该学Python?太纠结了?

首先&#xff0c;在大学期间的学习任务应该围绕自身的专业课程体系来展开&#xff0c;对于计算机相关专业的同学来说&#xff0c;学习Python还是有一定必要的&#xff0c;作为一门全场景编程语言&#xff0c;Python在大数据、人工智能等领域的应用还是比较广泛的&#xff0c;掌…

python中的列表,添加元素,获取元素,删除元素,列表分片,常用操作符

一. 创建列表&#xff0c;分为创建普通列表&#xff0c;混合列表&#xff0c;和空列表。其中混合列表是指string&#xff0c;int&#xff0c; float等都可以写在同一个列表里&#xff0c;空列表是指列表可以为空 二. 在列表添加成员方法 1. append&#xff08;&#xff09…

对于嵌入式交叉编译总结

这几天终于搞定了老师项目里我负责的部分&#xff0c;主要是做一个图像采集的手持端&#xff0c;我选用了JZ2440。 从移植内核、制作文件系统、Qt移植总结下来发现在对于代码的交叉编译必须保证编译平台的一致性。对于s3c2440来说是armv4t&#xff0c;所以当我们编译出来程序必…

Linux下svn搭建配置

Linux下svn搭建配置1、安装svn客户端yum install subversionsvnserve --version &#xff1b;如果成功安装&#xff0c;可以看到输出版本信息2、配置svn mkdir -p /data/svn &#xff1b;创建svn目录svnadmin create /dat…

rstudio 导出结果_RStudio如何完美导出包含中文的图

这篇文章源于我自己使用R及RStudio数据处理时遇到的问题&#xff0c;R非常强大&#xff0c;但是在中文支持方面还是不是很完美&#xff0c;比如遇到你想导出一个含有中文的图&#xff0c;就会遇到问题。比如有这样一个简单的图&#xff1a;data plot(data,xlimc(1,3),ylimc(2,3…

CSS进阶学习

5种主流浏览器及内核 IE trident Chrome webkit/blink Firefox gecko Opera presto 3%-5% Safari webkit css引入三种方式 行间样式 页面级 外部css文件 同步&#xff1a;顺序进行。一件事做完做另一件事。 异步&#xff1a;同时进行。两件不同的事同时做。 CSS权重&#xff…

linux驱动调试--oops信息

在移植dm9000 时被一个错误困扰了很久&#xff0c;当时手里只有printk调试手段&#xff0c;觉得自己应该升级下了&#xff0c;先学习了根据oops信息来调试。 先构造一个错误&#xff0c;insmod后抛出如下信息 我们着重看这几句 PC is at memcpy0x8c/0x29c c0148080 pc : …