iOS开发UI篇 -- UICollectionView

专业九宫格开发好几年。。。 必须使用这控件

这里以某易的“       产品推荐       ”界面做介绍。


一、UICollectionView的使用

1、注册cell(告诉collectionView将来创建怎样的cell)
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"product"];

2、从缓存池中取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
	UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath];

    return cell;
}
 
 3、重写init方法,创建布局参数 -- 》 这里重写init方法是因为我们在BaseViewController中都使用的是init方法,为了统一,所以重写了init方法
- (id)init
{
    // 1.流水布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 2.每个cell的尺寸
        layout.itemSize = CGSizeMake(100, 100);
    return [super initWithCollectionViewLayout:layout];
}

4、UICollectionViewFlowLayout称为”流水布局”, 用来约束cell的显示

常见属性
Cell的尺寸
@property (nonatomic) CGSize itemSize;

cell之间的水平间距
@property (nonatomic) CGFloat minimumInteritemSpacing;

cell之间的垂直间距
@property (nonatomic) CGFloat minimumLineSpacing;

四周的内边距
@property (nonatomic) UIEdgeInsets sectionInset;





二、上demo -- 某易的“       产品推荐       ”界面

#import  “BJProductViewController.h”

#define BJProductID @"product"

#import "BJProductViewController.h"
#import "BJProduct.h"
#import "BJCollectionCell.h"

@interface BJProductViewController ()

@property(nonatomic,strong)NSMutableArray *products;
@end

@implementation BJProductViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(85, 85);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(layout.minimumLineSpacing,10,0,10);
    return [super initWithCollectionViewLayout:layout];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (NSArray *)products{
    if(_products == nil){
        NSString *path = [[NSBundle mainBundle] pathForResource:@"products.json" ofType:nil];
        NSData *json = [NSData dataWithContentsOfFile:path];
        NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *mutArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            BJProduct *product = [BJProduct productWithDict:dict];
            [mutArray addObject:product];
        }
        
        _products = mutArray;
    }
    //    NSLog(@"%@",_products);
    return  _products;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    UINib *nib = [UINib nibWithNibName:@"BJCollectionCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:BJProductID];
    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.products.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    BJCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BJProductID forIndexPath:indexPath];
    
    // Configure the cell
    cell.product = self.products[indexPath.item];
    
    
    //    cell.backgroundColor = [UIColor orangeColor];
    return cell;
}

@end

#import BJCollectionCell.h

#import <UIKit/UIKit.h>

@class BJProduct;

@interface BJCollectionCell : UICollectionViewCell
@property(nonatomic,strong)BJProduct *product;
@end

#import BJCollectionCell.m

#import "BJCollectionCell.h"
#import "BJProduct.h"

@interface BJCollectionCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;

@property (weak, nonatomic) IBOutlet UILabel *nameLable;

@end

@implementation BJCollectionCell

- (void)awakeFromNib // 这里使用的是xib,在这里加载数据,还有需要注意cell的父类和ID
{
    // Initialization code
    self.iconImage.layer.cornerRadius = 6;
    self.iconImage.layer.masksToBounds = YES;
}

- (void)setProduct:(BJProduct *)product{
    _product = product;
    
    self.iconImage.image = [UIImage imageNamed:product.icon];
    self.nameLable.text = product.title;
}

@end

#import BJProduct.h

#import <Foundation/Foundation.h>

@interface BJProduct : NSObject
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *title;

+ (instancetype)productWithDict:(NSDictionary *)dict;
+ (instancetype)initWithDict:(NSDictionary *)dict;
@end

#import BJProduct.m

#import "BJProduct.h"

@implementation BJProduct
+ (instancetype)productWithDict:(NSDictionary *)dict{
    BJProduct *product = [[self alloc] init];
    product.icon = dict[@"icon"];
    product.title = dict[@"title"];
    return product;
}

+ (instancetype)initWithDict:(NSDictionary *)dict{
    return [self productWithDict:dict];
}
@end


















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中,支付是一个非常重要的功能。在开发支付时,需要考虑到支付流程、支付方式、支付安全等方面的内容。以下是一些开发支付的要点: 1. 集成支付SDK iOS开发中,一般使用第三方支付SDK来实现支付功能。常见的支付SDK包括:支付宝SDK、微信支付SDK、银联支付SDK等。在使用SDK前,需要先注册开发者账号,并获取相应的API Key和App ID等信息。 2. 支付流程 支付流程一般包括以下几个步骤: - 用户选择支付方式; - 向支付平台发起支付请求; - 用户输入支付密码; - 支付平台返回支付结果; - 应用根据支付结果进行相应的处理。 3. 支付安全 支付安全是非常重要的。在开发中,需要考虑到以下方面: - 用户信息的安全保护:包括用户的账号、密码、支付信息等; - 支付数据的安全保护:对于涉及到支付的数据,需要采用加密算法进行保护,避免被非法攻击者窃取; - 安全审计:需要对支付过程中的各个环节进行安全审计,及时发现并修复漏洞。 4. 支付方式 在iOS开发中,常见的支付方式包括: - 支付宝支付:支持PC端、移动端、扫码支付等多种支付方式; - 微信支付:支持微信内支付、H5支付、APP支付等多种支付方式; - 苹果支付:支持应用内购买,用户可以直接使用Apple ID进行支付。 需要根据应用的实际情况,选择适合的支付方式。 总之,开发支付需要考虑到多个方面的内容,包括支付流程、支付方式、支付安全等,需要仔细规划和实现,以保证支付功能的正常运作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值