控制器:

//  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. [bzoj2806][Ctsc2012]Cheat(后缀自动机(SAM)+二分答案+单调队列优化dp)

    偷懒直接把bzoj的网页内容ctrlcv过来了 2806: [Ctsc2012]Cheat Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1943   ...

  2. js计算最小凸多边形

    最近在做项目的时候遇到一个需求:要求用户可以在地图上绘制多边形,项目中使用的是高德地图,由于无法限制用户绘制的方式,可能出现下图的情况 用户期望的效果如下图所示 本质上,用户希望出现的是凸多边形而不是 ...

  3. String 工具类

    package com.mytripod.util; import sun.rmi.runtime.Log; import java.io.UnsupportedEncodingException; ...

  4. Huawei-R&S-网络工程师实验笔记20190615-IP基础(AR201上配置IP)

    >Huawei-R&S-网络工程师实验笔记20190615-IP基础(AR201上配置IP) >>实验开始,先上拓扑图参考: >>>一般正常配置IP操作如下 ...

  5. xtu summer individual 5 A - To Add or Not to Add

    To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

  6. 二元决策图(Binary decision diagram)

    修一门写作课题目是Binary decision diagram.在网上查了些资料感觉说的都不是很清楚,看了半天还是很困惑,这到底是个啥?到底咋使?于是决定写下这篇随笔,总结一下看过的各种资料加上自己 ...

  7. hihoCoder#1054 滑动解锁

    原题地址 回溯搜索 对于每个待枚举的点,检查: 1. 度数检查:是否违反了出度入度限制.因为生成的路径除了首尾节点外,其他节点的出度和入度只能为2 2. 共线检查:是否违反了共线条件.即跨越了尚未枚举 ...

  8. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

  9. [luoguP1044] 栈(数论?)

    传送门 卡特兰数 代码 #include <cstdio> int n; long long f[20]; int main() { int i; scanf("%d" ...

  10. PatentTips - Register file supporting transactional processing

    BACKGROUND OF THE INVENTION With the rise of multi-core, multi-threaded data processing systems, a k ...