自動(dòng)代碼布局
<#約束視圖#>.translatesAutoresizingMaskIntoConstraints = NO;
//
[<#父視圖#> addConstraint:[NSLayoutConstraint constraintWithItem:<#約束視圖#> attribute:(<#NSLayoutAttribute#>) relatedBy:(NSLayoutRelation<#Equal#>) toItem:<#參考視圖#> attribute:<#NSLayoutAttribute#> multiplier:<#倍數(shù)#> constant:<#基數(shù)#>]];
判斷線程
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(@"????<#說(shuō)明#>?%@", <#對(duì)象#>);
NSLog(@"????<#說(shuō)明#>?%@????<#說(shuō)明#>?%@", <#對(duì)象#>, <#對(duì)象#>);
NSLog(@"????\n函數(shù)名--->%s\n線程--->%@\n<#說(shuō)明#>--->%@", __func__, [NSThread currentThread], <#對(duì)象#>);
屬性
/// <#對(duì)象介紹#>
@property (nonatomic, assign)<#類名#> <#對(duì)象名#>;
/// <#對(duì)象介紹#>
@property (nonatomic, strong)<#類名#> *<#對(duì)象名#>;
主線程
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(self) strongSelf = weakSelf;
<#code#>
});
說(shuō)明
#pragma mark ------> <#說(shuō)明#>
#pragma mark ======================<#大標(biāo)題#>======================
單例
+ (<#類名#> *) shared<#類名#> {
static <#類名#> *o<#類名#> = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{//只執(zhí)行一次
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];
}
/**
設(shè)置tableView的數(shù)據(jù)源
*/
- (void)setupTableViewArray {
self.tableViewArr = [NSMutableArray arrayWithCapacity:0];
[_tableViewArr addObject:@{@"class":@"<#要push過(guò)去的類名#>", @"title":@"<#push過(guò)去的類的title#>", @"details":@"<#push類的細(xì)節(jié)描述#>"}];
}
/**
設(shè)置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);//分隔線間距(上, 左, 下, 右)
<#//不使用注冊(cè)方式的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 {
<#//不使用注冊(cè)方式的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的點(diǎn)擊事件
*/
- (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;
<#//不隱藏標(biāo)簽欄#>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;
多個(gè)按鈕的創(chuàng)建
/// 創(chuàng)建多個(gè)視圖(視圖寬高固定)
/// @param count 按鈕個(gè)數(shù)
/// @param superViewWidth 父視圖寬
/// @param width 寬
/// @param height 高
/// @param left 左邊距離
/// @param right 右邊距離
/// @param top 上邊距離
/// @param lineSpace 行間距(上下間距)
/// @param columnNumber 幾列(每行有幾個(gè))
/// @return 數(shù)組(里面是視圖)
- (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;//按鈕個(gè)數(shù)
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;//幾列(每行有幾個(gè))
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;//當(dāng)前視圖在第幾行
NSInteger tempc = (i % cn) + 1;//當(dāng)前視圖在第幾列
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;
}
/// 創(chuàng)建多個(gè)視圖(高度固定,寬度不固定)
/// @param count 按鈕個(gè)數(shù)
/// @param superViewWidth 父視圖寬
/// @param left 左邊距離
/// @param right 右邊距離
/// @param top 上邊距離
/// @param columnSpace 列間距(左右間距)
/// @param lineSpace 行間距(上下間距)
/// @param columnNumber 幾列(每行有幾個(gè))
/// @return 數(shù)組(里面是視圖)
- (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;//按鈕個(gè)數(shù)
CGFloat sw = superViewWidth;//父視圖寬
CGFloat h = height;//高
CGFloat l = left;//左邊距離
CGFloat r = right;//右邊距離
CGFloat t = top;//上邊距離
CGFloat cs = columnSpace;//列間距(左右間距)
CGFloat ls = lineSpace;//行間距(上下間距)
NSInteger cn = columnNumber;//幾列(每行有幾個(gè))
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;//當(dāng)前視圖在第幾行
NSInteger tempc = (i % cn) + 1;//當(dāng)前視圖在第幾列
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;
}
/// 創(chuàng)建多個(gè)按鈕
- (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);
}