iOS中大流中的自定义cell 技术分享
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 技术分享的更多相关文章
- iOS 在UITableViewCell中加入自定义view时view的frame设定注意
由于需要重用同一个布局,于是在cellForRowAtIndexPath中把自定义view加在了cell上,我是这样设定view的frame的 var screenFrame = UIScreen.m ...
- IOS 中关于自定义Cell 上的按钮 开关等点击事件的实现方法(代理)
1.在自定义的Cell .h文件中写出代理,写出代理方法. @protocol selectButtonDelegate <NSObject> -(void)selectModelID:( ...
- [iOS]技巧集锦:UITableView自定义Cell中的控件无法完全对齐Cell的左边界和右边界
这是个很诡异的问题,由于一些特殊需求,我的TableView的Cell的背景色是透明,其中的控件会有背景色,第一个控件和最后一个控件我都用IB自动设了约束,对齐Cell的左边界和右边界,但是自动约束很 ...
- iOS中 UISearchController 搜索栏 UI技术分享
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...
- iOS中 UITextView文本视图 技术分享
UITextView: 文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文. UITextField的用处多,UITextView的用法也不 ...
- iOS中 WGAFN_网络监控 技术分享
需要用到第三方AFNetworking/SVProgressHUD 没有的可以关注我微博私信我.http://weibo.com/hanjunqiang AppDelegate.m #import & ...
- 高大上的微信小程序中渲染html内容—技术分享
大部分Web应用的富文本内容都是以HTML字符串的形式存储的,通过HTML文档去展示HTML内容自然没有问题.但是,在微信小程序(下文简称为「小程序」)中,应当如何渲染这部分内容呢? 解决方案 wxP ...
- 李洪强iOS开发之自定义cell的使用
第一步: 创建自定义cell类,继承自UItableVIewcell 第二步: 在sb中布局自己需要的视图控件并且将此cell与我刚刚创建的cell类进行关联.并且连线 第三步: 创建modle类, ...
- ios中自定义cell 设置cell的分组结构
ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式 以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...
随机推荐
- React学习笔记(一)- 环境搭建
最近在学习react相关的知识,刚刚起步,一路遇坑不断.自己做个笔记,方便日后总结,也供相同趣味的小伙伴一起交流探讨. 学习时主要参考官网的教程:https://facebook.github.io/ ...
- intellij配置hibernate自动生成hbm.xml文件
1.首先创建一个Java web项目,这里因为已经在整个项目中配置好tomcat了,所以我是直接创建module的,其实和创建project的配置方法一样,创建的时候选择Web Application ...
- 借助Bodymovin播放svg动画
svg动画,截取工具有点不忍直视了~~~ 为了实现上面的svg动画,可以使用bodymovin插件,简单配置之后,就可以直接可以实现在 AE(可视化操作,不用码代码)上面导出 svg的json数据,在 ...
- hasattr(),getattr(),setattr()的使用
# 首先你有一个command.py文件,内容如下,这里我们假若它后面还有100个方法 class MyObject(object): def __init__(self): self.x = def ...
- Docker 第一篇 认识Docker 的作用好处
Docker 第一篇 认识Docker 的作用好处 (1)什么是Docker (2)Docker 优势劣势 Docker是去年开始关注并学习的,因为项目用到了AspnetCore 了解了之后总感觉会用 ...
- 四种方式实现子goroutine与主线程的同步
如何实现子goroutine与主线程的同步 第一种方式: 这种方式很太死板,就不演示了. 第二种方式:使用 channel机制,每个 goroutine传一个 channel进去然后往里写数据,在再主 ...
- ubuntu15.10 安装 virtualbox5.0
首先安装依赖包.ubuntu15.01安装的时候会出现这个错误: virtualbox-); however: Package libvpx1 is not installed 而且sudo apt- ...
- 如果将Joomla网站搜索结果显示到一个“干净”页面
有时候大家会发现Joomla网站自带的或者第三方的搜索功能时,搜索结果会显示在首页,和首页其它的模块如图片橱窗等显示在一起,非常混乱. 在这里教大家一个不需要修改代码的小技巧来解决这个问题,使搜索结果 ...
- 排序分析函数中对null的处理
--排序分析函数中对null的处理 --分析:对于null在分析函数中是升序默认是nulls last,降序默认是nulls first.如果不指定排序,那么是升序 )); ,'测试1'); ,'测试 ...
- 利用Python进行数据分析——Numpy基础:数组和矢量计算
利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...