首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITableViewController和延时执行、自定义控件

UITableViewController和延时执行、自定义控件

作者头像
tandaxia
发布2018-09-27 12:33:40
6190
发布2018-09-27 12:33:40
举报
文章被收录于专栏:谈补锅谈补锅

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];
    }
}

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构造方法
*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-05-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档