zoukankan      html  css  js  c++  java
  • UITableViewController和延时执行、自定义控件

    1、在UITableViewController中,self.view就是self.tableView, 两个对象的指针地址是一样的

    2、自定义类继承UITableViewCell, 重写父类方法  

    /** 
     初始化方法
     使用代码创建Cell的时候会被调用,如果使用XIB或者Storyboard,此方法不会被调用
     */
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            NSLog(@"%s", __func__);
        }
        return self;
    }
    
    /**
     从XIB被加载之后,会自动被调用,如果使用纯代码,不会被执行
     */
    - (void)awakeFromNib
    {
        NSLog(@"%s", __func__);
        self.contentView.backgroundColor = [UIColor clearColor];
    }
    
    /**
     Cell 被选中或者取消选中是都会被调用
     如果是自定义Cell控件,所有的子控件都应该添加到contentView中
     */
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];
    
        if (selected) {
            self.contentView.backgroundColor = [UIColor redColor];
        } else {
            self.contentView.backgroundColor = [UIColor greenColor];
        }
    }
    View Code

    3、延时执行命令,多线程GCD:

    //设置1秒钟后执行块中的代码
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         //执行代码
    });
    

      

    ----   补充一下  -----

    4、自定义View时,重写构造方法

    //使用代码创建对象的时候会调用这个方法,假如自定义View名称叫:MingView , 则:[MingView new]   / [[MingView alloc] init]     /  [[MingView alloc] initWithFrame: CGRectMake(10, 10, 10, 10)]] 最后都会调用重写的- (instancetype)initWithFrame:(CGRect)frame;

    - (instancetype)initWithFrame:(CGRect)frame;

     

    //使用xib创建或者用拖控件的方式创建自定义View对象,会调用到重写的- (instancetype)initWithCoder:(NSCoder *)aDecoder;

    - (instancetype)initWithCoder:(NSCoder *)aDecoder;

     

    举例:

    - (instancetype)init{
        NSLog(@"init ... 111 ");
        
        if (self = [super init]){
            NSLog(@"init ... 222 ");
        }
        return self;
    }
    
    - (instancetype)initWithFrame:(CGRect)frame{
        NSLog(@"initWithFrame ... 111");
        
        if (self = [super initWithFrame:frame]){
            NSLog(@"initWithFrame ... 222");
        }
        return self;
    }
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder{
        NSLog(@"initWithCoder ... 111 ");
        
        if (self = [super initWithCoder:aDecoder]){
            NSLog(@"initWithCoder ... 222 ");
        }
        return self;
    }
    /*
    1、纯代码创建该自定义对象
      a)、使用init、new来创建对象,打印日志:
           init ... 111 
           initWithFrame ... 111
           initWithFrame ... 222
           init ... 222 
       b)、使用initWithFrame来创建对象,打印日志:
           initWithFrame ... 111
           initWithFrame ... 222
       总结:纯代码创建对象都会调用initWithFrame构造方法
    
    2、使用xib或者拖控件的方式来创建对象
         大致同上,只是把initWithFrame改成initWithCoder
        总结:使用xib或拖控件的方式都会调用initWithCoder构造方法
    */
  • 相关阅读:
    Chrome下载无后缀问题
    duilib程序HelloWorld
    go的windows下的安装包
    ASP.NET MVC异步上传图片和富文本编辑器的使用详解
    搭建go环境(windows和linux)
    构建离线Go编程指南——gotour
    判定模块加载时是否存在循环依赖
    Winform开发框架之框架演化
    为什么要写技术博
    JTREE使用例子—创建一个客户端界面
  • 原文地址:https://www.cnblogs.com/tandaxia/p/UITableViewController.html
Copyright © 2011-2022 走看看