版权声明:本文为博主原创文章,未经博主允许不得转载。
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
return 300;
return 100;
点击cell触发的方法
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath
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;
@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;
最终效果如下:

原文地址:http://blog.csdn.net/qq_31810357/article/details/49611255