代碼塊

自動(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);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末键思,一起剝皮案震驚了整個(gè)濱河市钧排,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌匿级,老刑警劉巖顿乒,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件削樊,死亡現(xiàn)場(chǎng)離奇詭異青团,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)组哩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門等龙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人伶贰,你說(shuō)我怎么就攤上這事蛛砰。” “怎么了黍衙?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵泥畅,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我琅翻,道長(zhǎng)位仁,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任方椎,我火速辦了婚禮聂抢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘辩尊。我一直安慰自己涛浙,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布摄欲。 她就那樣靜靜地躺著轿亮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪胸墙。 梳的紋絲不亂的頭發(fā)上我注,一...
    開(kāi)封第一講書(shū)人閱讀 52,736評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音迟隅,去河邊找鬼但骨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛智袭,可吹牛的內(nèi)容都是我干的奔缠。 我是一名探鬼主播,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼吼野,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼校哎!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起瞳步,我...
    開(kāi)封第一講書(shū)人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤闷哆,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后单起,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體抱怔,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年嘀倒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屈留。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡测蘑,死狀恐怖绕沈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情帮寻,我是刑警寧澤乍狐,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站固逗,受9級(jí)特大地震影響浅蚪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜烫罩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一惜傲、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贝攒,春花似錦盗誊、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)荒适。三九已至,卻和暖如春开镣,著一層夾襖步出監(jiān)牢的瞬間刀诬,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工邪财, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留陕壹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓树埠,卻偏偏與公主長(zhǎng)得像糠馆,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子怎憋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容