指定根视图:
self.window.rootviewcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:[[roottableviewcontroller alloc] initwithstyle:uitableviewstyleplain]];
roottableviewcontroller.m
#import "wgmodel.h"
#import "wgcell.h"
@interface roottableviewcontroller ()
@property (nonatomic, strong) nsmutabledictionary *datadict;
@end
@implementation roottableviewcontroller
- (void)viewdidload
{
[super viewdidload];
self.datadict = [nsmutabledictionary dictionary];
[self.tableview registerclass:[wgcell class] forcellreuseidentifier:@"cell"];
[self loaddataandshow];
}
请求数据:
- (void)loaddataandshow
nsurl *url = [nsurl urlwithstring:@"http://api.breadtrip.com/trips/2387133727/schedule/"];
nsurlrequest *request = [nsurlrequest requestwithurl:url];
[nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) {
if (data != nil) {
nsarray *array = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:nil];
for (nsdictionary *dict in array) {
nsstring *key = dict[@"date"];
nsarray *placesarray = dict[@"places"];
nsmutablearray *mutablearray = [nsmutablearray array];
for (nsdictionary *placesdict in placesarray) {
wgmodel *model = [[wgmodel alloc] init];
[model setvaluesforkeyswithdictionary:placesdict];
model.isshow = no;
[mutablearray addobject:model];
}
[self.datadict setobject:mutablearray forkey:key];
}
[self.tableview reloaddata];
}
}];
#pragma mark - table view data source
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview
return self.datadict.allkeys.count;
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
nsstring *key = self.datadict.allkeys[section];
return [self.datadict[key] count];
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
wgcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath];
nsstring *key = self.datadict.allkeys[indexpath.section];
nsmutablearray *mutablearray = self.datadict[key];
wgmodel *model = mutablearray[indexpath.row];
[cell configurecellwithmodel:model];
if (model.isshow == yes) {
[cell showtableview];
} else {
[cell hiddentableview];
}
return cell;
自适应高
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath
if (model.isshow) {
return (model.pois.count + 1) * 44;
return 44;
点击cell会走的方法
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath
model.isshow = !model.isshow;
[self.tableview reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic];
自定义cell
//.h
#import <uikit/uikit.h>
@class wgmodel;
@interface wgcell : uitableviewcell<uitableviewdatasource, uitableviewdelegate>
@property (nonatomic, strong) uilabel *alabel;
@property (nonatomic, strong) uitableview *tableview;
- (void)configurecellwithmodel:(wgmodel *)model;
- (void)showtableview;
- (void)hiddentableview;
//.m
@interface wgcell ()
@property (nonatomic, strong) nsmutablearray *dataarray;
@implementation wgcell
- (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier
if (self = [super initwithstyle:style reuseidentifier:reuseidentifier]) {
self.dataarray = [nsmutablearray array];
[self addallviews];
return self;
- (void)addallviews
self.alabel = [[uilabel alloc] initwithframe:cgrectmake(0, 0, [uiscreen mainscreen].bounds.size.width, 44)];
self.alabel.backgroundcolor = [uicolor greencolor];
[self.contentview addsubview:self.alabel];
self.tableview = [[uitableview alloc] initwithframe:cgrectmake(0, 44, [uiscreen mainscreen].bounds.size.width, 0) style:uitableviewstyleplain];
self.tableview.delegate = self;
self.tableview.datasource = self;
[self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier:@"testcell"];
// [self.contentview addsubview:self.tableview];
- (void)showtableview
[self.contentview addsubview:self.tableview];
- (void)hiddentableview
[self.tableview removefromsuperview];
- (void)configurecellwithmodel:(wgmodel *)model
[self.dataarray removeallobjects];
self.alabel.text = model.place[@"name"];
nsarray *array = model.pois;
for (nsdictionary *dict in array) {
nsstring *str = dict[@"name"];
[self.dataarray addobject:str];
cgrect frame = self.tableview.frame;
frame.size.height = 444 * array.count;
self.tableview.frame = frame;
[self.tableview reloaddata];
return 1;
return self.dataarray.count;
uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"testcell" forindexpath:indexpath];
nsstring *str = self.dataarray[indexpath.row];
cell.textlabel.text = str;
准备一个model类
#import <foundation/foundation.h>
@interface wgmodel : nsobject
@property (nonatomic, assign) bool isshow;
@property (nonatomic, strong) nsdictionary *place;
@property (nonatomic, strong) nsarray *pois;
@implementation wgmodel
- (void)setvalue:(id)value forundefinedkey:(nsstring *)key
最终效果:

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