iOS开发学习周报(七)
简介
课程名称 | IOS开发实训 | 任课老师 | 郑贵锋老师&字节跳动工程师 |
---|---|---|---|
学号 | 16340015 | 专业(方向) | 软件工程(计应) |
姓名 | 陈彬彬 | 944131226@qq.com | |
开始日期 | 2019/04/27 | 完成日期 | 2019/05/02 |
文章目录
本周记录
0.概括
- UICollectionView基本使用
- UIImageView基本使用
- UIImageView异步加载网络图片
1.UICollectionView基本使用
参考:
概括:
相比于 UITableview
,UICollectionview
是 iOS6
之后引入的一个新的UI控件,它和 UITableview
有着许多的相似之处,但是它是一个比 UITableView
更加强大的一个视图控件,使用过程中需要实现数据源以及代理方法,其使用优点如下:
(1)系统自带的流水布局支持水平和垂直两种方式的布局;
(2)通过layout配置方式进行布局;
(3)collectionview中item的大小和位置可以自定义
(4)可以自定义一套layout的布局方案
关键代码:
ViewController.m
添加UICollectionView
并实现相关协议。
//
// ViewController.m
// week7-demo
//
// Created by chenbb6 on 2019/5/11.
// Copyright © 2019 chenbb6. All rights reserved.
//
#import "ViewController.h"
#import "CollectionViewCell.h"
@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) NSString *identifier;
@end
@implementation ViewController
- (instancetype)init
{
self = [super init];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize {
self.identifier = @"ReuseCell";
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadSubView];
}
- (void)loadSubView{
self.collectionView = ({
// 垂直布局
UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
fl.scrollDirection = UICollectionViewScrollDirectionVertical;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:fl];
[collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:self.identifier];
[collectionView setBackgroundColor:[UIColor whiteColor]];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView;
});
[self.view addSubview:self.collectionView];
}
# pragma mark -UICollectionViewDataSource
// cell样式设置
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *