• iOS ViewController 中代码书写规范


      示例:

    //
    //  CommonViewController.m
    //  ZTBCamera
    //
    //  Created by ZachRobin on 2017/8/3.
    //  Copyright © 2017年 Dreams of Ideal World Co.,Ltd. All rights reserved.
    //
    
    static const CGFloat customValue = 10;  //定义静态变量
    
    #define kScreenWidth  [[UIScreen mainScreen] bounds].size.width //定义
    
    #import "CommonViewController.h"
    
    #import "MagnumOpusCell.h"  //引入头文件
    #import "MagnumOpusDetailViewController.h"
    
    @interface CommonViewController () <UITableViewDelegate, UITableViewDataSource, MagnumOpusCellDelegate>
    {
        NSString *varableStr;//定义变量
    }
    
    @property (nonatomic, strong) UITableView *contentTableView;
    
    @end
    
    @implementation CommonViewController
    
    #pragma mark - ViewController Life Cycle
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 100, 100, 40);
        [button setTitle:@"按钮" forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor redColor]];
        [button addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }
    
    #pragma mark - System Delegate --
    #pragma mark - UITableViewDelegate / UITableViewDataSource
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 6;//_contentArray.count
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MagnumOpusCell *cell=[self.contentTableView dequeueReusableCellWithIdentifier:@"IDMagnumOpusCell"];
        if (!cell) {
            cell = [[MagnumOpusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IDMagnumOpusCell"];
        }
        [cell buildCellWithItem:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MagnumOpusCell *cell = (MagnumOpusCell *)[self tableView:self.contentTableView cellForRowAtIndexPath:indexPath];
        return [cell getCellHeight];
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"当前选中了 %ld", indexPath.row);
        MagnumOpusDetailViewController *VC = [[MagnumOpusDetailViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    #pragma mark - Customer Delegate
    - (void)removeCurrentCellData:(id)index
    {
        
    }
    
    #pragma mark - Event Response
    - (void)buttonDidClick:(id)sender
    {
        [self requestData];
    }
    
    #pragma mark - Private Method
    - (void)requestData
    {
        
    }
    
    #pragma mark - Getters / Setters
    - (UITableView *)contentTableView
    {
        if (!_contentTableView) {
            _contentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
            _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            _contentTableView.showsVerticalScrollIndicator = NO;
            _contentTableView.delegate = self;
            _contentTableView.dataSource = self;
            UINib *nib = [UINib nibWithNibName:@"MagnumOpusCell" bundle:[NSBundle mainBundle]];
            _contentTableView.backgroundColor = [UIColor clearColor];
            _contentTableView.showsVerticalScrollIndicator = NO;
            [_contentTableView registerNib:nib forCellReuseIdentifier:@"IDMagnumOpusCell"];
        }
        return _contentTableView;
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end

      附件:示例代码。

  • 相关阅读:
    【定位不准的烦心事系列】第2篇:卫星信号弱到底是咋回事
    高德「渲染练习生计划」启动,开启专业渲染技术人旅程!
    Mybatis第三方PageHelper分页插件原理
    List集合分页处理的方法
    什么是ETCD及其应用场景
    获取不到http请求头自定义参数
    etcd和Zookeeper孰优孰劣对比
    Zookeeper应用场景汇总(超详细)
    List集合分页处理的方法
    国产化之x64平台安装银河麒麟操作系统
  • 原文地址:https://www.cnblogs.com/ZachRobin/p/7280994.html
Copyright © 2020-2023  润新知