AppDelegate.m指定根视图

 self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];

//根视图

RootTableViewController.m

#import "RootTableViewController.h"
#import "TestCell.h"
#import "TestModel.h"

@interface RootTableViewController ()

@property (nonatomic, strong) NSMutableArray *datasourceArray;

@end

@implementation RootTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.datasourceArray = [NSMutableArray array];

    [self.tableView registerClass:[TestCell class] forCellReuseIdentifier:@"cell"];

    for (int i = 0; i < 50; i++) {
        TestModel *model = [TestModel new];
        model.isShow = NO;
        [self.datasourceArray addObject:model];
    }

}

#pragma mark - Table view data source

数据源方法

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.datasourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    TestModel *model = self.datasourceArray[indexPath.row];

    if (model.isShow) {

        cell.label.text = @"展示view";
        [cell addView];
    } else {
        cell.label.text = @"什么都没有";
        [cell removeView];
    }

    return cell;
}

返回高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestModel *model = self.datasourceArray[indexPath.row];
    if (model.isShow) {
        return 300;
    } else {
        return 100;
    }
}

点击cell触发的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestModel *model = self.datasourceArray[indexPath.row];
    model.isShow = !model.isShow;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

准备一个自定义cell

#import <UIKit/UIKit.h>

@interface TestCell : UITableViewCell

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *redView;

- (void)addView;
- (void)removeView;

@end

#import "TestCell.h"

@implementation TestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllViews];
    }
    return self;
}

- (void)addAllViews
{
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    self.label.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.label];

    self.redView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 200)];
    self.redView.backgroundColor = [UIColor redColor];

}

- (void)addView
{
    [self addSubview:self.redView];
}

- (void)removeView
{
    [self.redView removeFromSuperview];
}

准备一个model类

#import <Foundation/Foundation.h>

@interface TestModel : NSObject

@property (nonatomic, assign) BOOL isShow;

@end

最终效果如下:







有好的建议和问题可微博私信:http://weibo.com/hanjunqiang

iOS中大流中的自定义cell 技术分享的更多相关文章

  1. iOS 在UITableViewCell中加入自定义view时view的frame设定注意

    由于需要重用同一个布局,于是在cellForRowAtIndexPath中把自定义view加在了cell上,我是这样设定view的frame的 var screenFrame = UIScreen.m ...

  2. IOS 中关于自定义Cell 上的按钮 开关等点击事件的实现方法(代理)

    1.在自定义的Cell .h文件中写出代理,写出代理方法. @protocol selectButtonDelegate <NSObject> -(void)selectModelID:( ...

  3. [iOS]技巧集锦:UITableView自定义Cell中的控件无法完全对齐Cell的左边界和右边界

    这是个很诡异的问题,由于一些特殊需求,我的TableView的Cell的背景色是透明,其中的控件会有背景色,第一个控件和最后一个控件我都用IB自动设了约束,对齐Cell的左边界和右边界,但是自动约束很 ...

  4. iOS中 UISearchController 搜索栏 UI技术分享

    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...

  5. iOS中 UITextView文本视图 技术分享

    UITextView: 文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文. UITextField的用处多,UITextView的用法也不 ...

  6. iOS中 WGAFN_网络监控 技术分享

    需要用到第三方AFNetworking/SVProgressHUD 没有的可以关注我微博私信我.http://weibo.com/hanjunqiang AppDelegate.m #import & ...

  7. 高大上的微信小程序中渲染html内容—技术分享

    大部分Web应用的富文本内容都是以HTML字符串的形式存储的,通过HTML文档去展示HTML内容自然没有问题.但是,在微信小程序(下文简称为「小程序」)中,应当如何渲染这部分内容呢? 解决方案 wxP ...

  8. 李洪强iOS开发之自定义cell的使用

    第一步: 创建自定义cell类,继承自UItableVIewcell 第二步: 在sb中布局自己需要的视图控件并且将此cell与我刚刚创建的cell类进行关联.并且连线  第三步: 创建modle类, ...

  9. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

随机推荐

  1. django.db.utils.ProgrammingError: 1146 的解决办法

    在models中设置完数据库相关的东西后执行命令 python manage.py makemigrations 此处无错误 再次执行 python manage.py migrate 发生报错 错误 ...

  2. C++ 实现俄罗斯方块

    C++ 实现俄罗斯方块 一.实验介绍 1.1 实验内容 本节实验我们进行设计俄罗斯方块前的思路分析,以及介绍ncurses 库的使用方法. 1.2 实验知识点 C++ 编程基础 ncurses 库的使 ...

  3. centos6.8下weblogic12c静默安装

    环境: centos6.8 无桌面环境 jdk1.7.0_25 关闭iptables.selinux 安装前准备: 1.新建weblogic用户,设置weblogic密码 useradd weblog ...

  4. 使用Docker搭建GitLab

    使用docker-compose快速启动GitLab.(当然前提是你先安装docker-compose,安装方式见博客:http://blog.csdn.net/yulei_qq/article/de ...

  5. OpenResty 执行阶段的概念和用途

    主要还是 Nginx 的执行阶段知识了,都是因为 OR 才会那么深刻, 它有些自己的阶段. 主要还是参照 春哥的 Nginx 教程 请多读几遍,如果不清楚nginx的执行阶段就无法充分利用 openr ...

  6. MYSQL 更新时间自动同步与创建时间默认值共存问题

    本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50326259 在使用SQL的时候,希望在更新数据的时候自动填充更新 ...

  7. scheme深拷贝和浅拷贝探索

    > (define a '(1 2 3)) > (define b (cons a '())) > b (( )) > (set-car! (car b) ) > b ( ...

  8. 关于[[NSNotificationCenter defaultCenter] addObserver不remove后续又收到通知crash问题

    今天试了一个小demo,测出一个现象,同步出来:object 作为 observer 监听了通知 A,然后 object 中途被释放执行了dealloc,随后app发出这个通知 A:iOS 6.iOS ...

  9. 好用的有多种样式的搜索界面创建UISearchBar

    之前看到一个别人封装的第三方PYSearch,相当于一个完整的搜索界面,今天在这里进行代码说明一下. 将PYSearch拖进项目或者使用Pods进行加库,我是直接拖进项目中进行使用 PYSearch库 ...

  10. 全文检索 Lucene(3)

    看完前两篇博客之后,想必大家对于Lucene的使用都有了一个比较清晰的认识了.如果对Lucene的知识点还是有点模糊的话,个人建议还是先看看这两篇文章. 全文检索 Lucene(1) 全文检索 Luc ...