iOS中大流中的自定义cell 技术分享
2015-11-03 09:15
302 查看
AppDelegate.m指定根视图
[objc] view
plaincopy
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];
//根视图
RootTableViewController.m
[objc] view
plaincopy
#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
数据源方法
[objc] view
plaincopy
#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;
}
返回高
[objc] view
plaincopy
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestModel *model = self.datasourceArray[indexPath.row];
if (model.isShow) {
return 300;
} else {
return 100;
}
}
点击cell触发的方法
[objc] view
plaincopy
- (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
[objc] view
plaincopy
#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类
[objc] view
plaincopy
#import <Foundation/Foundation.h>
@interface TestModel : NSObject
@property (nonatomic, assign) BOOL isShow;
@end
最终效果如下:
[objc] view
plaincopy
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];
//根视图
RootTableViewController.m
[objc] view
plaincopy
#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
数据源方法
[objc] view
plaincopy
#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;
}
返回高
[objc] view
plaincopy
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestModel *model = self.datasourceArray[indexPath.row];
if (model.isShow) {
return 300;
} else {
return 100;
}
}
点击cell触发的方法
[objc] view
plaincopy
- (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
[objc] view
plaincopy
#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类
[objc] view
plaincopy
#import <Foundation/Foundation.h>
@interface TestModel : NSObject
@property (nonatomic, assign) BOOL isShow;
@end
最终效果如下:
相关文章推荐
- ios 读取通讯录
- iOS中 通知中心Text (实例)
- iOS中 通知中心Text (实例)
- iOS中大流中的自定义cell 技术分享
- iOS中大流中的自定义cell 技术分享
- ios消息队列APNS实现和证书申请
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- 蓝懿ios 技术内容和心得交流分享 11.2 打飞机主要步骤
- iOS数据持久化之归档
- iOS MBProgressHUD 的使用方法
- iOS 多线程
- iOS地图分析
- CoreData(1)
- iOS学习必备的7个开源项目
- iOS多线程GCD
- ios调用web service
- iOS资源图片命名注意事项
- iOS模拟器上home键调用方法(2种)
- iOS CoreData之单表操作(增删改查)
- IOS中获取各种文件的目录路径