控制器:

//  XMGViewController.h

#import <UIKit/UIKit.h>

@interface XMGViewController : UIViewController

@end
//  XMGViewController.m

#import "XMGViewController.h"
#import "XMGShop.h"
#import "XMGShopView.h" @interface XMGViewController () // 购物车
@property (weak, nonatomic) IBOutlet UIView *shopCarView;
// 添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
// 删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeButton; /** 数据数组 */
@property (nonatomic, strong) NSArray *dataArr;
@end @implementation XMGViewController
/**
* 懒加载,get方法
*/
- (NSArray *)dataArr{
if (_dataArr == nil) {
// 加载数据
// 1.获取全路径
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"shopData.plist" ofType:nil];
self.dataArr = [NSArray arrayWithContentsOfFile:dataPath];
// 字典转模型
// 创建临时数组
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in _dataArr) {
// 创建shop对象
XMGShop *shop = [XMGShop shopWithDict:dict];
// 把模型装入数组
[tempArray addObject:shop];
}
self.dataArr = tempArray;
}
return _dataArr;
} // 初始化数据
- (void)viewDidLoad {
[super viewDidLoad];
} /**
* 添加到购物车
*
* @param button 按钮
*/
- (IBAction)add:(UIButton *)button {
/***********************1.定义一些常量*****************************/
// 1.总列数
NSInteger allCols = ;
// 2.商品的宽度 和 高度
CGFloat width = ;
CGFloat height = ;
// 3.求出水平间距 和 垂直间距
CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width) / (allCols -);
CGFloat vMargin = (self.shopCarView.frame.size.height - * height) / ;
// 4. 设置索引
NSInteger index = self.shopCarView.subviews.count;
// 5.求出x值
CGFloat x = (hMargin + width) * (index % allCols);
CGFloat y = (vMargin + height) * (index / allCols); /***********************2.创建一个商品*****************************/
/*
XMGShopView *shopView = [[XMGShopView alloc] init];
shopView.frame = CGRectMake(x, y, width, height);
// 设置数据
XMGShop *shop = self.dataArr[index];
shopView.shop = shop;
[self.shopCarView addSubview:shopView];
*/ XMGShopView *shopView = [XMGShopView shopViewWithShop:self.dataArr[index]];
shopView.frame = CGRectMake(x, y, width, height);
[self.shopCarView addSubview:shopView]; /***********************3.设置数据*****************************/
// 设置数据
// XMGShop *shop = self.dataArr[index];
// [shopView setIcon:shop.icon];
// [shopView setName:shop.name]; /***********************4.设置按钮的状态*****************************/ button.enabled = (index != ); // 5.设置删除按钮的状态
self.removeButton.enabled = YES; } /**
* 从购物车中删除
*
* @param button 按钮
*/
- (IBAction)remove:(UIButton *)button {
// 1. 删除最后一个商品
UIView *lastShopView = [self.shopCarView.subviews lastObject];
[lastShopView removeFromSuperview]; // 3. 设置添加按钮的状态
self.addButton.enabled = YES; // 4. 设置删除按钮的状态
self.removeButton.enabled = (self.shopCarView.subviews.count != ); }
@end

自定义控件:

//  XMGShopView.h

#import <UIKit/UIKit.h>
@class XMGShop; @interface XMGShopView : UIView /** 商品模型 */
@property (nonatomic, strong) XMGShop *shop; // 构造方法
- (instancetype)initWithShop: (XMGShop *)shop;
+ (instancetype)shopViewWithShop: (XMGShop *)shop; @end
//  XMGShopView.m

#import "XMGShopView.h"
#import "XMGShop.h" @interface XMGShopView ()
/** 图片控件 */
@property (nonatomic, weak) UIImageView *iconView;
/** 标题控件 */
@property (nonatomic, weak) UILabel *titleLabel;
@end @implementation XMGShopView /**
* 初始化子控件(不要设置frame)
*
*/
- (instancetype)init{
if (self = [super init]) {
[self setUp];
}
return self;
} //构造方法,对象方法
- (instancetype)initWithShop:(XMGShop *)shop{
if (self = [super init]) {
// 注意:先创建后赋值,不然空指针,
[self setUp];
self.shop = shop;//调用set方法
}
return self;
}
//构造方法,类方法
+ (instancetype)shopViewWithShop:(XMGShop *)shop{
return [[self alloc] initWithShop:shop];
} /**
* 初始化
*/
- (void)setUp{
// 1.创建商品的UIImageView对象
UIImageView *iconView = [[UIImageView alloc] init];
iconView.backgroundColor = [UIColor blueColor];
[self addSubview:iconView];
_iconView = iconView; // 2.创建商品标题对象
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
[self addSubview:titleLabel];
_titleLabel = titleLabel;
} /**
* 布局子控件(可以拿到frame)
*/
- (void)layoutSubviews{
// 0.一定要调用super
[super layoutSubviews]; // 1.获取当前控件的尺寸
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height; // 2.设置子控件的frame
self.iconView.frame = CGRectMake(, , width, width);
self.titleLabel.frame = CGRectMake(, width, width, height - width);
} /**
* 重写set方法:只要外边传数据就会调用
* 作用:设置数据
*/
- (void)setShop:(XMGShop *)shop{
_shop = shop; // 设置数据
self.iconView.image = [UIImage imageNamed:shop.icon];
self.titleLabel.text = shop.name;
} @end

bean:

//  XMGShop.h

#import <Foundation/Foundation.h>

@interface XMGShop : NSObject

/** 图片的名称 */
@property (nonatomic, copy) NSString *icon;
/** 商品的名称 */
@property (nonatomic, copy) NSString *name; // 提供构造方法
/*
- (instancetype)initWithIcon: (NSString *)icon name: (NSString *)name;
+ (instancetype)shopWithIcon: (NSString *)icon name: (NSString *)name;
*/ - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)shopWithDict:(NSDictionary *)dict; @end
//  XMGShop.m

#import "XMGShop.h"

@implementation XMGShop
/*
- (instancetype)initWithIcon:(NSString *)icon name:(NSString *)name{
if (self = [super init]) {
self.icon = icon;
self.name = name;
}
return self;
} + (instancetype)shopWithIcon:(NSString *)icon name:(NSString *)name{
return [[self alloc] initWithIcon:icon name:name];
}
*/ - (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
self.icon = dict[@"icon"];
self.name = dict[@"name"];
}
return self;
} + (instancetype)shopWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
} @end

ios18---自定义控件3改进的更多相关文章

  1. Qt编写自定义控件大全

    最新版可执行文件 http://pan.baidu.com/s/1i491FQP 不定期增加控件及修正BUG和改进算法. 总图: 1:动画按钮 * 1:可设置显示的图像和底部的文字 * 2:可设置普通 ...

  2. Qt编写自定义控件二动画按钮

    现在的web发展越来越快,很多流行的布局样式,都是从web开始的,写惯了Qt widgets 项目,很多时候想改进一下现有的人机交互,尤其是在现有的按钮上加一些动画的效果,例如鼠标移上去变大,移开还原 ...

  3. QT自定义控件系列(二) --- Loading加载动画控件

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...

  4. Qt自定义控件系列(一) --- 圆形进度条

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...

  5. Asp.net自定义控件开发任我行(附1)-属性一览众山小

    元数据属性应用于服务器控件及其成员,从而提供由设计工具.ASP.NET 页分析器.ASP.NET 运行库以及公共语言运行库使用的信息.当页开发人员在可视化设计器中使用控件时,设计时属性能改进开发人员的 ...

  6. Asp.net自定义控件开发任我行(2)-TagPrefix标签

    摘要 前面我们已经做了一个最简单的TextBox的马甲,此篇文章,我们来讲讲自定义控件的标签.大家可能看到了上一篇中拖放进来的代码是 <cc1:TextEdit ID="TextEdi ...

  7. Qt编写自定义控件63-水波效果

    一.前言 几年前就一直考虑过写这个控件了,在9年前用C#的时候,就看到过别人用C#写了个水波效果的控件,挺好玩的,当时看了下代码用的二维数组来存储变换的图像像素数据,自从学了Qt以后,有过几次想要用Q ...

  8. C#用户自定义控件(含源代码)-透明文本框

    using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...

  9. WPF教程十二:了解自定义控件的基础和自定义无外观控件

    这一篇本来想先写风格主题,主题切换.自定义配套的样式.但是最近加班.搬家.新租的房子打扫卫生,我家宝宝6月中旬要出生协调各种的事情,导致了最近精神状态不是很好,又没有看到我比较喜欢的主题风格去模仿的, ...

  10. android自定义控件一站式入门

    自定义控件 Android系统提供了一系列UI相关的类来帮助我们构造app的界面,以及完成交互的处理. 一般的,所有可以在窗口中被展示的UI对象类型,最终都是继承自View的类,这包括展示最终内容的非 ...

随机推荐

  1. [Luogu] P1865 A % B Problem

    题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...

  2. vijos1382寻找主人

    题目大意: 给出两个串(长度<=1e6),问是否同构,如果同构输出最小表示. 题解: 这是最小表示法模板题.在这里好好讲一下最小表示法. 首先有一个最暴力的方法:把所有表示搞出来排序. 时间复杂 ...

  3. linux如何正确设置静态ip

    如果是新安装的CentOS7的用户,刚开始应该是没网的,ifconfig命令在CentOS7已经被淘汰了. 1.使用ip addr 即查看分配网卡情况. 2.激活网卡 [root@localhost ...

  4. 树莓派 -- oled 续(1) wiringPi

    在上文中,分析了wiringPi 的oled demo是使用devfs来控制spi master和spi slave通讯. https://blog.csdn.net/feiwatson/articl ...

  5. Django中配置自定义日志系统

  6. sscanf,sprintf

    sprintf函数 sprintf函数原型为 int sprintf(char str, const char format, ...).作用是格式化字符串,具体功能如下所示: 将数字变量转换为字符串 ...

  7. 对Spring框架的理解(转)

    ①  spring框架是一个开源而轻量级的框架,是一个IOC和AOP容器 ② spring的核心就是控制反转(IOC)和面向切面编程(AOP) ③  控制反转(IOC):是面向对象编程中的一种设计原则 ...

  8. NOR flash and NAND flash

    (1)读写的基本单位 应用程序对NOR芯片操作以"字"为基本单位.为了方便对大容量NOR闪存的管理,通常将NOR闪存分成大小为128KB或者64KB的逻辑块,有时候块内还分成扇区. ...

  9. 易维信(EVTrust)支招五大技巧识别钓鱼网站

    网上购物和网上银行凭借其便捷性和通达性,在互联网上日渐流行.在互联网上,你可以随时进行转账汇款或进行交易.据艾瑞咨询发布<2008-2009年中国网上支付行业发展报告>显示:中国互联网支付 ...

  10. [NOIP2006] 提高组 洛谷P1065 作业调度方案

    题目描述 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作,我们用记号j-k表示一个 ...