代码块

作者: 扣肉快快跑 | 来源:发表于2019-08-05 16:50 被阅读0次

自动代码布局

<#约束视图#>.translatesAutoresizingMaskIntoConstraints = NO;

//
[<#父视图#> addConstraint:[NSLayoutConstraint constraintWithItem:<#约束视图#> attribute:(<#NSLayoutAttribute#>) relatedBy:(NSLayoutRelation<#Equal#>) toItem:<#参考视图#> attribute:<#NSLayoutAttribute#> multiplier:<#倍数#> constant:<#基数#>]];

判断线程

if ([[NSThread currentThread] isMainThread]) {
        
    } else {
        __weak typeof(self) weakSelf = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            __strong typeof(self) strongSelf = weakSelf;
            
        });
    }

打印

NSLog(@"action--->%s", __func__);
NSLog(@"🥳🥳<#字符串#>");
NSLog(@"🥳🥳<#说明#>☞%@", <#对象#>);
NSLog(@"🥳🥳<#说明#>☞%@🐳🐳<#说明#>☞%@", <#对象#>, <#对象#>);
NSLog(@"🥳🥳\n函数名--->%s\n线程--->%@\n<#说明#>--->%@", __func__, [NSThread currentThread], <#对象#>);

属性

 /// <#对象介绍#>
@property (nonatomic, assign)<#类名#> <#对象名#>;
/// <#对象介绍#>
@property (nonatomic, strong)<#类名#> *<#对象名#>;

主线程

__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
    __strong typeof(self) strongSelf = weakSelf;
    <#code#>
});

说明

#pragma mark ------> <#说明#>
#pragma mark ======================<#大标题#>======================

单例

+ (<#类名#> *) shared<#类名#> {
    static <#类名#> *o<#类名#> = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{//只执行一次
        o<#类名#> = [[<#类名#> alloc] init];
    });
    return o<#类名#>;
}

tableView

@interface <#类名#> ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray<__kindof NSDictionary *> *tableViewArr;
@end

static NSString *<#类名#>CellID = @"<#类名#>Cell";
@implementation <#类名#>

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationItem.title = @"<#此类的title#>";
    [self setupTableViewArray];
    [self settingTableViewWay];
    
}
/**
 设置tableView的数据源
 */
- (void)setupTableViewArray {
    self.tableViewArr = [NSMutableArray arrayWithCapacity:0];
    [_tableViewArr addObject:@{@"class":@"<#要push过去的类名#>", @"title":@"<#push过去的类的title#>", @"details":@"<#push类的细节描述#>"}];
}

/**
 设置tableView
 */
- (void)settingTableViewWay {
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:_tableView];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.separatorColor = [UIColor orangeColor];//分隔线的颜色
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);//分隔线间距(上, 左, 下, 右)
    <#//不使用注册方式的cell#>[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:<#类名#>CellID];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
}
#pragma mark - UITableViewDataSource

/**
 返回section的row
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableViewArr.count;
}

/**
 返回indexPath的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    <#//不使用注册方式的cell#>UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#类名#>CellID forIndexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#类名#>CellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:<#类名#>CellID];
    }
    cell.textLabel.text = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.detailTextLabel.text = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"details"];
    return cell;
}
#pragma mark - UITableViewDelegate

/**
 cell的点击事件
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *wayString = @"push";
    if ([wayString isEqualToString:@"push"]) {
        NSString *classString = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"class"];
        NSString *titleString = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"title"];
        Class tempClass = NSClassFromString(classString);
        UIViewController *vc = [[tempClass alloc] init];
        vc.view.backgroundColor = [UIColor whiteColor];
        vc.navigationItem.title = titleString;
        <#//不隐藏标签栏#>vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([wayString isEqualToString:@"方法"]) {
        NSString *selString = [NSString stringWithFormat:@"selectRowAtIndexPath%zd", indexPath.row];
        SEL selector = NSSelectorFromString(selString);
        IMP imp = [self methodForSelector:selector];
        void (*func)(id, SEL) = (void (*)(id,SEL))imp;
        func(self,selector);
    } else {
        
    }
}
- (void)selectRowAtIndexPath<#row#> {

}
@end

弱引用

__weak typeof(self) weakSelf = self;
__strong typeof(self) strongSelf = weakSelf;

多个按钮的创建

/// 创建多个视图(视图宽高固定)
 
/// @param count 按钮个数
/// @param superViewWidth 父视图宽
/// @param width 宽
/// @param height 高
/// @param left 左边距离
/// @param right 右边距离
/// @param top 上边距离
/// @param lineSpace 行间距(上下间距)
/// @param columnNumber 几列(每行有几个)
/// @return 数组(里面是视图)
- (NSArray *)setupViewsWithCount:(NSInteger)count
                  superViewWidth:(CGFloat)superViewWidth
                           width:(CGFloat)width
                          height:(CGFloat)height
                            left:(CGFloat)left
                           right:(CGFloat)right
                             top:(CGFloat)top
                       lineSpace:(CGFloat)lineSpace
                    columnNumber:(NSInteger)columnNumber {
    
    NSInteger c = count;//按钮个数
    CGFloat sw = superViewWidth;//父视图宽
    CGFloat w = width;//宽
    CGFloat h = height;//高
    CGFloat l = left;//左边距离
    CGFloat r = right;//右边距离
    CGFloat t = top;//上边距离
    CGFloat cs = 0;//列间距(左右间距)
    CGFloat ls = lineSpace;//行间距(上下间距)
    NSInteger cn = columnNumber;//几列(每行有几个)
    NSInteger ln = 0;//几行
    cs = (sw - l - r - cn * w) / ((cn - 1) * 1.0);
    if (l + r + cs + w > sw) {
        cn = 1;
    } else {
        cn = (sw - r - l + cs) / (w + cs);
    }
    if (c % cn > 0) {
        ln = c / cn + 1;
    } else {
        ln = c / cn;
    }
    
    NSMutableArray *viewArray = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < c; i++) {
        NSInteger templ = (i / cn) + 1;//当前视图在第几行
        NSInteger tempc = (i % cn) + 1;//当前视图在第几列
        CGFloat xxx = l + (cs + w) * (tempc - 1);
        CGFloat yyy =  t + (ls + h) * (templ - 1);
        CGFloat www = w;
        CGFloat hhh = h;
        
        CGRect frame = CGRectMake(xxx, yyy, www, hhh);
        NSString *title = [NSString stringWithFormat:@"%ld", (long)i];
        [viewArray addObject:[self setupButtonWithFrame:frame title:title target:self action:@selector(buttonsFunc:) cornerRadius:5 borderWidth:1 borderColor:[UIColor orangeColor]]];
    }
    return viewArray;
}
/// 创建多个视图(高度固定,宽度不固定)
/// @param count 按钮个数
/// @param superViewWidth 父视图宽
/// @param left 左边距离
/// @param right 右边距离
/// @param top 上边距离
/// @param columnSpace 列间距(左右间距)
/// @param lineSpace 行间距(上下间距)
/// @param columnNumber 几列(每行有几个)
/// @return 数组(里面是视图)
- (NSArray *)setupViewsWithCount:(NSInteger)count
                  superViewWidth:(CGFloat)superViewWidth
                          height:(CGFloat)height
                            left:(CGFloat)left
                           right:(CGFloat)right
                             top:(CGFloat)top
                     columnSpace:(CGFloat)columnSpace
                       lineSpace:(CGFloat)lineSpace
                    columnNumber:(NSInteger)columnNumber {
    
    NSInteger c = count;//按钮个数
    CGFloat sw = superViewWidth;//父视图宽
    CGFloat h = height;//高
    CGFloat l = left;//左边距离
    CGFloat r = right;//右边距离
    CGFloat t = top;//上边距离
    CGFloat cs = columnSpace;//列间距(左右间距)
    CGFloat ls = lineSpace;//行间距(上下间距)
    NSInteger cn = columnNumber;//几列(每行有几个)
    NSInteger ln = 0;//几行
    CGFloat w = (sw - l - r - (cn * cs) + cs)  / (cn * 1.0);//宽
    if (c % cn > 0) {
        ln = c / cn + 1;
    } else {
        ln = c / cn;
    }
    //    NSLog(@"ln--->%ld行--->宽%ld", (long)ln, (long)w);
    NSMutableArray *viewArray = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < c; i++) {
        NSInteger templ = (i / cn) + 1;//当前视图在第几行
        NSInteger tempc = (i % cn) + 1;//当前视图在第几列
        CGFloat xxx = l + (cs + w) * (tempc - 1);
        CGFloat yyy =  t + (ls + h) * (templ - 1);
        CGFloat www = w;
        CGFloat hhh = h;
        //        NSLog(@"%ld__%ld行__%ld列", (long)i, (long)templ, (long)tempc);
        CGRect frame = CGRectMake(xxx, yyy, www, hhh);
        NSString *title = [NSString stringWithFormat:@"%ld", (long)i];
        [viewArray addObject:[self setupButtonWithFrame:frame title:title target:self action:@selector(buttonsFunc:) cornerRadius:5 borderWidth:1 borderColor:[UIColor orangeColor]]];
    }
    return viewArray;
}

/// 创建多个按钮
- (UIView *)setupButtonWithFrame:(CGRect)frame title:(NSString *)title target:(nullable id)target action:(SEL)action cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    if (frame.size.width) {
        button.frame = frame;
    }
    if (borderColor) {
        button.layer.borderColor = borderColor.CGColor;
    }
    
    if (borderWidth) {
        button.layer.borderWidth = borderWidth;
    }
    if (cornerRadius) {
        button.layer.cornerRadius = cornerRadius;
    }
    if (title) {
        [button setTitle:title forState:(UIControlStateNormal)];
    }
    if (target && action) {
        [button addTarget:target action:action forControlEvents:(UIControlEventTouchUpInside)];
    }
    return button;
}
- (void)buttonsFunc:(UIButton *)button {
    NSLog(@"🥳🥳%@", button);
}

相关文章

  • 普通代码块、构造代码块、静态代码块、同步代码块

    普通代码块:在方法或语句中出现的{}就称为普通代码块。普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定-...

  • Java中静态代码块,构造代码块,构造函数代码块

    Java中静态代码块,构造代码块,构造函数代码块,普通代码块 静态代码块 : static代码块指的是static...

  • 09.代码块的概述

    代码块 局部代码块 局部代码块是定义在方法或语句中 构造代码块 构造代码块是定义在类中成员位置的代码块 静态代码块...

  • Java代码块详解

    Java中代码块指的是用 {} 包围的代码集合,分为4种:普通代码块,静态代码块,同步代码块,构造代码块 普通代码...

  • java中的代码块

    java中的代码块: 普通代码块,静态代码块,构造代码块 1. 普通代码块:在方法或者语句中, …… ...

  • Java基础之普通代码块、构造代码块、静态代码块、同步代码块

    1、概念 普通代码块:在方法或语句中出现的{}就称为普通代码块。普通代码块和一般的语句执行顺序由他们在代码中出现的...

  • 代码块

    rem

  • 代码块

    案例 邮箱:ithelei@sina.cn 技术讨论群:687856230 GoodLuck

  • 代码块

    代码块:1.普通代码块,在方法中写的代码块2.构造快,在类中定义的代码块,在创建对象时被调用,优于构造方法执行3....

  • 代码块

    定义:被“{}”包括的代码片段,使代码具备独立性,一般用于实现特定算法; 分类:普通代码块、静态代码块、同步代码块...

网友评论

      本文标题:代码块

      本文链接:https://www.haomeiwen.com/subject/yzfmdctx.html