一. UIPickerView——小案例:点菜系统
//
// ViewController.m
// UIPickerView
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource> {
NSArray *array_table; //桌号
NSArray *array1; //主食
NSArray *array2; //主菜
NSArray *array3; //饮料
UIPickerView *mpickerView;
NSString *tableNum;
NSString *mainFood;
NSString *mainDish;
NSString *drink;
}
@property (weak, nonatomic) IBOutlet UILabel *lab_mainFood;//主食
@property (weak, nonatomic) IBOutlet UILabel *lab_mainDish;//主菜
@property (weak, nonatomic) IBOutlet UILabel *lab_drink;
@property (weak, nonatomic) IBOutlet UIButton *buttonSubmit;
- (IBAction)foodSubmit:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
array_table = [[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];
array1 = @[@"米饭",@"面条",@"窝窝头"];
array2 = @[@"青椒肉丝",@"油焖大虾",@"大闸蟹"];
array3 = @[@"莫吉托",@"柠檬柑橘",@"蜜桃气泡水",@"苏打水"];
mpickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 414, 200)];
mpickerView.delegate = self;
mpickerView.dataSource = self;
[self.view addSubview:mpickerView];
}
// 几个部分,几竖排
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 4;
}
// 每个部分由多少行组成
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return array_table.count;
} else if (component == 1) {
return array1.count;
} else if (component == 2) {
return array2.count;
} else if (component == 3) {
return array3.count;
} else {
return 0;
}
}
// 每个cell的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *str = nil;
if (component == 0) {
str = [array_table objectAtIndex:row];
} else if (component == 1) {
str = [array1 objectAtIndex:row];
} else if (component == 2) {
str = [array2 objectAtIndex:row];
} else if (component == 3) {
str = [array3 objectAtIndex:row];
}
return str;
}
// 选择某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
tableNum = [array_table objectAtIndex:row];
} else if (component == 1) {
mainFood = [array1 objectAtIndex:row];
} else if (component == 2) {
mainDish = [array2 objectAtIndex:row];
} else if (component == 3) {
drink = [array3 objectAtIndex:row];
}
}
- (IBAction)foodSubmit:(id)sender {
[self.buttonSubmit setTitle:[NSString stringWithFormat:@"第%@桌",tableNum] forState:UIControlStateNormal];
self.lab_mainFood.text = mainFood;
self.lab_mainDish.text = mainDish;
self.lab_drink.text = drink;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
二. UICollectionView
UICollectionView = UITableView + cell(非固定的)
UITableView适用于N行单列,而UICollectionView 适用于N行N列
UICollectionView管理有序的数据项集合,提出了使用定制的布局
UICollectionView能够具体布局对象的布局信息,非常方便的显示数据
UICollectionView继承自UIScrollView
1. UICollectionView之九宫格布局
//
// ViewController.m
// UICollectionView
// head cell foot: 颜色 内容 高度
// 1 size
// 2 注册
// 3 head和foot使用一个共同的方法
#import "ViewController.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource> {
NSMutableArray *array_color;
UIColor *color_copy;
UICollectionView *mcollectionView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
array_color = [[NSMutableArray alloc]init];
for (int i = 0; i<10; i++) {
[array_color addObject:[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0]];
}
//设置约束条件
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//size: item head foot
layout.itemSize = CGSizeMake(130, 130);
layout.headerReferenceSize = CGSizeMake(414, 50);
layout.footerReferenceSize = CGSizeMake(414, 20);
//方向 垂直滑动
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
mcollectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
mcollectionView.delegate = self;
mcollectionView.dataSource = self;
//cell的复用ID需要注册一下!
//注册: item head foot
[mcollectionView registerClass:[UICollectionViewCell class ] forCellWithReuseIdentifier:@"cellID"];
[mcollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headID"];
[mcollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footID"];
[self.view addSubview:mcollectionView];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handlelong:)];
[mcollectionView addGestureRecognizer:longPress];
}
- (void)handlelong:(UILongPressGestureRecognizer *)longPress {
switch (longPress.state) {
case UIGestureRecognizerStateBegan:{
// 找到item index
// 点击在什么位置 locationInView
NSIndexPath *indexPath = [mcollectionView indexPathForItemAtPoint:[longPress locationInView:mcollectionView]];
if(indexPath == nil){
break;
}
// 在该路径上开始移动
[mcollectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
case UIGestureRecognizerStateChanged:{
// 更新
[mcollectionView updateInteractiveMovementTargetPosition:[longPress locationInView:mcollectionView]];
}
break;
case UIGestureRecognizerStateEnded:{
// 关闭 移动
[mcollectionView endInteractiveMovement];
}
break;
default:
[mcollectionView cancelInteractiveMovement];
break;
}
}
// 1 几个组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
// 2 每组里有多少item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return array_color.count;
}
// 3 item内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//随机颜色
UIColor *color = [array_color objectAtIndex:indexPath.row];
cell.backgroundColor = color;
return cell;
}
// 4 head和foot共用的方法 kind:head foot
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headID" forIndexPath:indexPath];
if (headerView == nil) {
headerView = [[UICollectionReusableView alloc]init];
}
headerView.backgroundColor = [UIColor redColor];
return headerView;
} else if ([kind isEqualToString: UICollectionElementKindSectionFooter]) {
UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footID" forIndexPath:indexPath];
if (footView == nil) {
footView = [[UICollectionReusableView alloc]init];
}
footView.backgroundColor = [UIColor blueColor];
return footView;
} else {
return nil;
}
}
/*
// 5 选中 删除
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
//选中变红色
cell.backgroundColor = [UIColor redColor];
//删除
[array_color removeObjectAtIndex:indexPath.row];
[collectionView deleteItemsAtIndexPaths:@[indexPath]];
}
// 6 copy
// 6.1 长按 弹出菜单 6.2 使能 6.3 操作
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return true;
}
// 6.2 使能复制粘贴操作
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
// action 知道到底是哪个方法 paste:粘贴
if ([NSStringFromSelector(action) isEqualToString:@"copy:"] ||
[NSStringFromSelector(action) isEqualToString:@"paste:"]) {
return true;
} else {
return false;
}
}
// 6.3 操作
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if ([NSStringFromSelector(action) isEqualToString:@"copy:"]) {
color_copy = [array_color objectAtIndex:indexPath.row];
} else if ([NSStringFromSelector(action) isEqualToString:@"paste:"]) {
if (color_copy != nil) {
[array_color insertObject:color_copy atIndex:indexPath.row];
color_copy = nil;
[collectionView insertItemsAtIndexPaths:@[indexPath]];
}
}
}
*/
// 7 item任意位置拖动
// 7.1 item 使能 7.2 刷新(数组,View)7.3手势控制
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
return true;
}
//7.2 刷新(颜色数组,UICollectionView)
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
id obj = [array_color objectAtIndex:sourceIndexPath.item];
//如果不知道是什么类型可以用id
[array_color removeObject:obj];
[array_color insertObject:obj atIndex:destinationIndexPath.item];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
九宫格升级版
//
// ViewController.m
// UICollectionView
// head cell foot: 颜色 内容 高度
// 1 size
// 2 注册
// 3 head和foot使用一个共同的方法
#import "ViewController.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
//设置约束条件
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//方向 垂直滑动
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
UICollectionView *mcollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(50, 0, 414-100, self.view.frame.size.height) collectionViewLayout:layout];
mcollectionView.delegate = self;
mcollectionView.dataSource = self;
//cell的复用ID需要注册一下!
//注册: item head foot
[mcollectionView registerClass:[UICollectionViewCell class ] forCellWithReuseIdentifier:@"cellID"];
[mcollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headID"];
[mcollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footID"];
[self.view addSubview:mcollectionView];
}
//设置每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row%2==0) {
return CGSizeMake(50, 50);
}else {
return CGSizeMake(100, 100);
}
}
// 1 几个组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
// 2 每组里有多少item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
}
// 3 item内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//随机颜色
UIColor *color = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0];
cell.backgroundColor = color;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2. 小案例——瀑布流布局及自定义layout
//
// ViewController.m
// UICollectionView瀑布流
//1 Model:图片名称,宽高 2 View 3 Collection
#import "ViewController.h"
#import "MyImage.h"
#import "MyCollectionViewLayout.h"
#import "MyCollectionViewCell.h"
@interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate,MyCollectionViewLayoutDelegate>{
NSMutableArray *sourceData;
UICollectionView *mcollectionView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
sourceData = [[NSMutableArray alloc]init];
NSArray *array_height = @[@"275",@"300",@"270",@"265",@"270",@"354"];
for (int i = 0; i < 6; i++) {
MyImage *mImage = [[MyImage alloc]init];
mImage.image = [NSString stringWithFormat:@"%d.jpg",i]; //名字
NSString *height = [array_height objectAtIndex:i];
mImage.height = height.floatValue;
mImage.width = 200;
[sourceData addObject:mImage];
}
MyCollectionViewLayout *layout = [[MyCollectionViewLayout alloc]init];
layout.delegate = self;
mcollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) collectionViewLayout:layout];
mcollectionView.dataSource = self;
mcollectionView.delegate = self;
mcollectionView.backgroundColor = [UIColor whiteColor];
[mcollectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
[self.view addSubview:mcollectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [MyCollectionViewCell createCellWithCollectionView:collectionView indexPath:indexPath];
MyImage *item = [sourceData objectAtIndex:indexPath.row];
[cell loadData:item];
return cell;
}
- (float)getCellHeight:(float)width index:(int)index {
MyImage *item = [sourceData objectAtIndex:index];
return width*item.height/item.width;//根据item的宽高比例和宽得到高
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// MyImage.h
// UICollectionView瀑布流
#import <Foundation/Foundation.h>
@interface MyImage : NSObject
@property (nonatomic,copy) NSString *image;
@property (nonatomic,assign) float width;
@property (nonatomic,assign) float height;
@end
//
// MyCollectionViewCell.h
// UICollectionView瀑布流
#import <UIKit/UIKit.h>
#import "MyImage.h"
@interface MyCollectionViewCell : UICollectionViewCell
+ (instancetype)createCellWithCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath*)indexPath;
- (void)loadData:(MyImage *)mImage;
@end
//
// MyCollectionViewCell.m
// UICollectionView瀑布流
#import "MyCollectionViewCell.h"
@interface MyCollectionViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation MyCollectionViewCell
+ (instancetype)createCellWithCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath*)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
return cell;
}
- (void)loadData:(MyImage *)mImage {
self.imageView.image = [UIImage imageNamed:mImage.image];
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
@end
//
// MyCollectionViewLayout.h
// UICollectionView瀑布流
#import <UIKit/UIKit.h>
//自定义协议
@protocol MyCollectionViewLayoutDelegate<NSObject>
- (float)getCellHeight:(float)width index:(int)index;
@end
@interface MyCollectionViewLayout : UICollectionViewLayout
@property(nonatomic,weak)id<MyCollectionViewLayoutDelegate> delegate;
@end
//
// MyCollectionViewLayout.m
// UICollectionView瀑布流
#import "MyCollectionViewLayout.h"
@interface MyCollectionViewLayout() {
NSMutableArray *attsArray; //cell约束
NSMutableArray *itemHeight; //高度
}
@end
@implementation MyCollectionViewLayout
// 1 初始化
- (void)prepareLayout {
[super prepareLayout];
if (attsArray == nil) {
attsArray = [[NSMutableArray alloc]init];
}
if (itemHeight == nil) {
itemHeight = [[NSMutableArray alloc]init];
}
//获取有多少个item
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i < count; i++) {
//indexPath item section
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
//返回每一个layout约束
UICollectionViewLayoutAttributes *att = [self layoutAttributesForItemAtIndexPath:indexPath];
[attsArray addObject:att];
}
}
// 2 滚动范围
- (CGSize)collectionViewContentSize {
return CGSizeMake(0, 0);
}
// 3 return 属性数组
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
return attsArray;
}
// 4 设置约束内容
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGFloat collectionViewW = self.collectionView.frame.size.width;
CGFloat w = (collectionViewW-10*4)/3;
CGFloat h = [self.delegate getCellHeight:w index:(int)indexPath.item];
CGFloat x = 10 + (indexPath.row%3)*(w+10);
CGFloat y = 10;
if (indexPath.row < 3) {
itemHeight[indexPath.row] = [NSNumber numberWithFloat:h];
}
if (indexPath.row >= 3) {
NSNumber *num_h = itemHeight[indexPath.row%3];
y = 10 + num_h.floatValue + 10;
}
att.frame = CGRectMake(x, y, w, h);
return att;
}
@end
三. APP常用架构结构介绍
四. iOS9新特性
1. UIAlertController基本使用
//
// ViewController.m
// UIAlertViewController
#import "ViewController.h"
@interface ViewController () {
UIAlertController *alertController;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
//Style:UIAlertControllerStyleAlert UIAlertControllerStyleActionSheet
alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:true completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2. UIAlertController用户名密码操作
//
// ViewController.m
// UIAlertViewController
#import "ViewController.h"
@interface ViewController () {
UIAlertController *alertController;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"用户名";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = true;
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//点击按钮之后执行
UITextField *username = alertController.textFields.firstObject;
UITextField *password = alertController.textFields.lastObject;
NSLog(@"%@ %@",username.text,password.text);
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:true completion:nil];
}
@end
3. UIActionSheet
//
// ViewController.m
// UIAlertViewController
//
// Created by 李姝谊 on 2018/9/26.
// Copyright © 2018年 李姝谊. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () {
UIAlertController *alertController;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:true completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end