iOS十种常用代码片

把常用代码写入代码块, 输入快捷单词就能弹出对应的代码段

目录
1. 设置 UI
2. 四种类型的懒加载
3. 创建 tableview
4. 创建 collectionview
5. UICollectionViewCell的创建
6. UITableViewCell 的创建
7. 复用 cell 的 id
8. 网络库的初始化
9. 给分类添加属性
10. 添加属性
11. YYModel数据校验
12. 添加每个类的 pragma
13.  collectionview的header

1. 设置 UI

setupUI

#import 
#import 
#import "MJRefresh.h"
@property (nonatomic,strong) UIImageView *<#actBgImageView#>;

- (void)setupControl{
    [self _initializeViews];
    [self _makeConstraints];
    [self _initializeBaseConfig];
}

- (void)_initializeBaseConfig{
}

- (void)_initializeViews {
    [self addSubview:<#self.actBgImageView#>];
}

- (void)_makeConstraints {
    [<#self.actBgImageView#> mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(<#self#>).offset(fitIPHONE6Width(15));
        make.left.mas_equalTo(<#self#>).offset(fitIPHONE6Width(15));
        make.bottom.mas_equalTo(<#self#>).offset(fitIPHONE6Width(-15));
        make.right.mas_equalTo(<#self#>).offset(fitIPHONE6Width(-15));
    }];
}

2. 四种类型的懒加载

lazy_create

#import "PTVSkinManager.h"
#import "UIFixRegionButton.h"
#import 
#import 

@property (nonatomic,strong) UIView *<#middleView#>;
@property (nonatomic,strong) UIImageView *<#actBgImageView#>;
@property (nonatomic,strong) UIFixRegionButton *<#bottomBtn#>;
@property (nonatomic,strong) UILabel *<#actTipsLabel#>;

- (UIView *)<#middleView#> {
if (!_<#middleView#>) {
UIView *middleView = [[UIView alloc] init];
middleView.backgroundColor = [UIColor whiteColor];
_<#middleView#> = middleView;
}
return _<#middleView#>;
}
- (UIImageView *)<#actBgImageView#> {
if (!_<#actBgImageView#>) {
UIImage *img = [[PTVSkinManager sharedManager] loadImageNoCache:@"carteam/车队查房消息提醒弹板"];
UIEdgeInsets edge = UIEdgeInsetsMake(10, 10, 10, 10);
img = [img resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
imageView.userInteractionEnabled = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
_<#actBgImageView#> = imageView;
}
return _<#actBgImageView#>;
}

- (UIFixRegionButton *)<#bottomBtn#> {
if (!_<#bottomBtn#>) {
UIFixRegionButton *button = [UIFixRegionButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = [[PTVConfig instance] boldFont:18 * kViewWidthRate];
[button setTitle:@"发车队红包" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(onBottomBtnClick) forControlEvents:UIControlEventTouchUpInside];
_<#bottomBtn#> = button;
[self enableSendBtnState:NO];
}
return _<#bottomBtn#>;
}

- (void)enableSendBtnState:(BOOL)enabel{
    if (enabel) {
        UIImage *img = [UIImage imageNamed:@"carteam_icon_s"];
        img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(0, img.size.width * 0.5, 0, img.size.width * 0.5) resizingMode:UIImageResizingModeStretch];
        [_<#bottomBtn#> setBackgroundImage:img forState:UIControlStateNormal];
    } else {
        UIImage *img = [[PTVSkinManager sharedManager] loadImageNoCache:@"carteam2nd/车队-统一确认大按钮不可点击"];
        UIEdgeInsets edge = UIEdgeInsetsMake(0, img.size.width/2.0, 0, img.size.width/2.0);
        img = [img resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];
        [_<#bottomBtn#> setBackgroundImage:img forState:UIControlStateNormal];
    }
    _<#bottomBtn#>.enabled = enabel;
}

- (void)onBottomBtnClick{
    
}

- (UILabel *)<#actTipsLabel#> {
if (!_<#actTipsLabel#>) {
UILabel *label = [UILabel new];
label.textColor = [PTVConfig themeColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [[PTVConfig instance] normalFont:18 * kViewWidthRate];
label.text = @"车队查房";
label.numberOfLines = 1;
_<#actTipsLabel#> = label;
}
return _<#actTipsLabel#>;
}

3. 创建 tableview

add_tableview

#import 
#import 
#import "MJRefresh.h"

@property (nonatomic,weak) UITableView *<#competitionTableView#>;
@property (nonatomic,strong) NSArray *listDataArray;

#pragma mark listView

- (UITableView *)competitionTableView
{
    if (_<#competitionTableView#> == nil)
    {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        tableView.backgroundColor = <#KBgColor#>;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.delegate = self;
        tableView.dataSource = self;
        WeakSelf
        tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf requestMatchScheduleNoLoadingView];
        }];
        tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf requestMatchScheduleMoreDataNoLoadingView];
        }];
        //        tableView.allowsSelection = NO;
        tableView.sectionFooterHeight = <#5#>;
        tableView.sectionHeaderHeight = <#0#>;
        tableView.rowHeight = <#304.5#>;
        tableView.showsVerticalScrollIndicator = NO;
        
        UIView *tableViewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 8)];
        tableViewHeader.backgroundColor = <#KBgColor#>;
        tableView.tableHeaderView= tableViewHeader;
        
        [tableView registerClass:[<#PTVCompetitionListViewCell#> class] forCellReuseIdentifier:[<#PTVCompetitionListViewCell#> reuseId]];
        [self addSubview:tableView];
        _<#competitionTableView#> = tableView;
    }
    return _<#competitionTableView#>;
}

/**
 *  上拉刷新
 */
- (void)requestMatchScheduleMoreDataNoLoadingView
{
    WeakSelf
    [<#_mgr#> requestMatchScheduleMoreData:^(BOOL succ, NSArray<__kindof PTVCompetitionData *> *array,BOOL isLastPage) {
//        [weakSelf _analyzeMoreScheduleData:succ array:array];
        [weakSelf _showRefreshFinishedView:isLastPage];
    }];
}

/**
 *  下拉刷新
 */
- (void)requestMatchScheduleNoLoadingView
{
    WeakSelf
    [<#_mgr#> requestMatchSchedule:^(BOOL succ, NSArray<__kindof PTVCompetitionData *> *array,BOOL isLastPage) {
        if (succ)
        {
            weakSelf.listDataArray = array;
            [weakSelf.<#competitionTableView#> reloadData];
        }
        [weakSelf _showRefreshFinishedView:isLastPage];
    }];
}

- (void)_showRefreshFinishedView:(BOOL)isLastPage
{
    if (isLastPage)
    {
        [self.<#competitionTableView#>.mj_footer endRefreshingWithNoMoreData];
    }
    else
    {
        [self.<#competitionTableView#>.mj_footer endRefreshing];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _listDataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    <#PTVCompetitionListViewCell#> *cell = [tableView dequeueReusableCellWithIdentifier:[<#PTVCompetitionListViewCell#> reuseId] forIndexPath:indexPath];
    <#<#PTVCompetitionListViewCell#>#> *data = _listDataArray[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    <#PTVCompetitionListViewCell#> *cell = [tableView cellForRowAtIndexPath:indexPath];
//    [cell pushVideoController];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return <#7#>;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    if (section == _listDataArray.count - 1)
    {
        view.backgroundColor = <#KBgColor#>;
    }
    else
    {
        view.backgroundColor = <#kSubSeparatorLineBgColor#>;
    }
    return view;
}

4. 创建 collectionview

add_collectionview

#import 
#import 
#import "Masonry.h"

@property (nonatomic,strong) NSArray *logoImgArray;
@property (nonatomic,strong) UICollectionView *<#collectionview#>;

#pragma mark `collectionview set data`
- (void)setLogoImgArray:(NSArray *)logoImgArray
{
    _logoImgArray = logoImgArray;
    [self.<#collectionview#> reloadData];
}

#pragma mark `collectionview lazy load`
- (UICollectionView *)<#collectionview#>{
    if (_<#collectionview#> == nil) {
        UICollectionView *collectionview = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[UICollectionViewFlowLayout new]];
        [collectionview registerClass:[<#LuckydrawLogoCollectionViewCell#> class] forCellWithReuseIdentifier:[<#LuckydrawLogoCollectionViewCell#> reuseId]];
        collectionview.delegate = self;
        collectionview.dataSource = self;
        UIView *containerView = self;
        [containerView addSubview:collectionview];
        [collectionview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(containerView);
            make.size.mas_equalTo(CGSizeMake(fitIPHONE6Width(<#300#>), fitIPHONE6Width(<#300#>)));
            
        }];
        _<#collectionview#> = collectionview;
    }
    return _<#collectionview#>;
}

#pragma mark 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.logoImgArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    <#LuckydrawLogoCollectionViewCell#> *cell = (<#LuckydrawLogoCollectionViewCell#> *)[collectionView dequeueReusableCellWithReuseIdentifier:[<#LuckydrawLogoCollectionViewCell#> reuseId] forIndexPath:indexPath];
    UIImage *img = [self.logoImgArray objectAtIndex:indexPath.row];
    //    [cell setLogoImg:img];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(<#50#>, <#50#>);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, <#10#>, <#10#>, <#10#>);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return <#10#>;
}

5. UICollectionViewCell的创建

add_collectionviewCell

#import 
#import 
#import "PTVSkinManager.h"
#import "Masonry.h"

@property (nonatomic,strong) UILabel *<#titleLabel#>;
@property (nonatomic,strong) UIImageView *<#logoImgView#>;

- (void)setLogoImg:(NSString *)localImgName{
    UIImage *img = [[PTVSkinManager sharedManager] loadImageNoCache:localImgName];
    _<#logoImgView#>.image = img;
}

- (void)setTitle:(NSString *)text{
    _<#titleLabel#>.text = text;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupControl];
    }
    return self;
}

- (void)setupControl{
    [self _initializeBaseConfig];
    [self _initializeViews];
    [self _makeConstraints];
}

- (void)_initializeBaseConfig{
    self.backgroundColor = [UIColor whiteColor];
    self.contentView.backgroundColor = [UIColor whiteColor];
}

- (void)_initializeViews {
    [self.contentView addSubview:self.<#logoImgView#>];
    [self.contentView addSubview:self.<#titleLabel#>];
}

- (void)_makeConstraints {
    [self.<#logoImgView#> mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(40);
    }];
    [self.<#titleLabel#> mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.<#logoImgView#>);
        make.bottom.right.mas_equalTo(self.contentView);
    }];
}

- (UIImageView *)<#logoImgView#> {
    if (!_<#logoImgView#>) {
        UIImageView *imageView = [[UIImageView alloc] init];
        _<#logoImgView#> = imageView;
    }
    return _<#logoImgView#>;
}

- (UILabel *)<#titleLabel#> {
    if (!_<#titleLabel#>) {
        UILabel *label = [UILabel new];
        label.textColor = UIColorFromRGB(0x333333);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [[PTVSConfig instance] normalFont:11];
        _<#titleLabel#> = label;
    }
    return _<#titleLabel#>;
}

6. UITableViewCell 的创建, 和 UICollectionViewCell的创建基本一致, 只是初始化方法名称不一样.

add_tableviewCell

#import 
#import 
#import "PTVSkinManager.h"
#import "Masonry.h"

@property (nonatomic,strong) UILabel *<#titleLabel#>;
@property (nonatomic,strong) UIImageView *<#logoImgView#>;

- (void)setLogoImg:(NSString *)localImgName{
    UIImage *img = [[PTVSkinManager sharedManager] loadImageNoCache:localImgName];
    _<#logoImgView#>.image = img;
}

- (void)setTitle:(NSString *)text{
    _<#titleLabel#>.text = text;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupControl];
    }
    return self;
}

- (void)setupControl{
    [self _initializeBaseConfig];
    [self _initializeViews];
    [self _makeConstraints];
}

- (void)_initializeBaseConfig{
    self.backgroundColor = [UIColor whiteColor];
    self.contentView.backgroundColor = [UIColor whiteColor];
}

- (void)_initializeViews {
    [self.contentView addSubview:self.<#logoImgView#>];
    [self.contentView addSubview:self.<#titleLabel#>];
}

- (void)_makeConstraints {
    [self.<#logoImgView#> mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(40);
    }];
    [self.<#titleLabel#> mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.<#logoImgView#>);
        make.bottom.right.mas_equalTo(self.contentView);
    }];
}

- (UIImageView *)<#logoImgView#> {
    if (!_<#logoImgView#>) {
        UIImageView *imageView = [[UIImageView alloc] init];
        _<#logoImgView#> = imageView;
    }
    return _<#logoImgView#>;
}


- (UILabel *)<#titleLabel#> {
    if (!_<#titleLabel#>) {
        UILabel *label = [UILabel new];
        label.textColor = UIColorFromRGB(0x333333);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [[PTVSConfig instance] normalFont:11];
        _<#titleLabel#> = label;
    }
    return _<#titleLabel#>;
}

7. 复用 cell 的 id

reuseid_func

+ (NSString *)reuseId
{
    return NSStringFromClass([self class]);
}

8. 网络库的初始化

network_create

- (id)init {
    if (self = [super init]) {
        _network = [[PTVHttpNetwork alloc] init];
        [_network responseJSONserializer];
        [_network setSpecialHeader];
    }
    return self;
}

9. 给分类添加属性

category_property

- (PTVAnchorPKManager *)getAnchorPkMgr
{
    if ([self anchorPkMgr] == nil)
    {
        [self setAnchorPkMgr:[PTVAnchorPKManager new]];
    }
    return [self anchorPkMgr];
}

- (PTVAnchorPKManager *)anchorPkMgr{
    PTVAnchorPKManager *mgr = objc_getAssociatedObject(self, @selector(anchorPkMgr));
    return mgr;
}

- (void)setAnchorPkMgr:(PTVAnchorPKManager *)anchorPkMgr{
    objc_setAssociatedObject(self, @selector(anchorPkMgr), anchorPkMgr, OBJC_ASSOCIATION_RETAIN);
}

10. 添加属性

@property_strong

property (nonatomic,strong) UIView *sideView;

11. YYModel数据校验

YYModelCheck

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"des" : @"info.des",
             @"arrayGiftData": @"gift"};
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"arrayGiftData" : [GoddessGiftData class]};
}
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
    return YES;
}

12. 添加每个类的 pragma

#pragma mark - Life Circle 生命周期

#pragma mark - Hik<#Class#>Delegate <#代理#>

#pragma mark - Event Response 手势/事件等

#pragma mark - Public Methods 公开的接口

#pragma mark - Private Methods 私有方法

#pragma mark - Override 重写的方法

#pragma mark - Notification 通知

#pragma mark - Setters and Getters 

13. collectionview的header

- (void)registHeader {
    [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHotSearchListCollectionViewTitleViewReuseId];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *headerLabel = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHotSearchListCollectionViewTitleViewReuseId forIndexPath:indexPath];
    [headerLabel addSubview:self.headerLabel];
        
    [self.headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(headerLabel);
        make.left.mas_equalTo(12);
        make.height.mas_equalTo(40);
    }];
    return headerLabel;
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, 40);
}

- (UIView *)headerLabel {
    if (_headerLabel == nil) {
        UILabel *label = [UILabel new];
        label.textAlignment= NSTextAlignmentLeft;
        label.font = HIK_FONT_MEDIUM(14);
        label.text = @"热门搜索";
        label.textColor = HIK_COLOR_HEX(0x333333);
        label.numberOfLines = 1;
        _headerLabel = label;
    }
    return _headerLabel;
}

14、layer_corner_radius_lz

        button.layer.cornerRadius = 24;
        button.layer.masksToBounds = YES;

15、layer_border_lz

        button.layer.borderColor = PTV_COLOR_THEME_RED.CGColor;
        button.layer.borderWidth = 1;

16、addsubview_lz

[<#self#> addSubview:self.<#myModelLabel#>];

你可能感兴趣的:(iOS十种常用代码片)