iOS中 自定义cell升级版 (高级)
ios 自定义 高级 cell 升级版
2023-09-14 08:59:40 时间
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];
[self.tableView registerClass:[WGCell class] forCellReuseIdentifier:@"cell"]; [self 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]; } }];
- (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 NSString *key = self.dataDict.allKeys[indexPath.section]; NSMutableArray *mutableArray = self.dataDict[key]; WGModel *model = mutableArray[indexPath.row]; if (model.isShow) { return (model.pois.count + 1) * 44; } else { return 44; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath NSString *key = self.dataDict.allKeys[indexPath.section]; NSMutableArray *mutableArray = self.dataDict[key]; WGModel *model = mutableArray[indexPath.row]; model.isShow = !model.isShow; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
@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; @end //.m #import "WGCell.h" #import "WGModel.h" @interface WGCell () @property (nonatomic, strong) NSMutableArray *dataArray; @end @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]; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView return 1; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section return self.dataArray.count; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath]; NSString *str = self.dataArray[indexPath.row]; cell.textLabel.text = str; return cell;
@property (nonatomic, assign) BOOL isShow; @property (nonatomic, strong) NSDictionary *place; @property (nonatomic, strong) NSArray *pois; @end //.m #import "WGModel.h" @implementation WGModel - (void)setValue:(id)value forUndefinedKey:(NSString *)key @end
RootTableViewController.m
[self.tableView registerClass:[WGCell class] forCellReuseIdentifier:@"cell"]; [self 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]; } }];
- (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 NSString *key = self.dataDict.allKeys[indexPath.section]; NSMutableArray *mutableArray = self.dataDict[key]; WGModel *model = mutableArray[indexPath.row]; if (model.isShow) { return (model.pois.count + 1) * 44; } else { return 44; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath NSString *key = self.dataDict.allKeys[indexPath.section]; NSMutableArray *mutableArray = self.dataDict[key]; WGModel *model = mutableArray[indexPath.row]; model.isShow = !model.isShow; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
@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; @end //.m #import "WGCell.h" #import "WGModel.h" @interface WGCell () @property (nonatomic, strong) NSMutableArray *dataArray; @end @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]; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView return 1; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section return self.dataArray.count; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath]; NSString *str = self.dataArray[indexPath.row]; cell.textLabel.text = str; return cell;
@property (nonatomic, assign) BOOL isShow; @property (nonatomic, strong) NSDictionary *place; @property (nonatomic, strong) NSArray *pois; @end //.m #import "WGModel.h" @implementation WGModel - (void)setValue:(id)value forUndefinedKey:(NSString *)key @end
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
原文地址:http://blog.csdn.net/qq_31810357/article/details/49847711相关文章
- iOS单元测试的那些事儿
- Android与IOS渲染流程对比
- 手机端复制文本的封装js-兼容ios与安卓
- iOS Crash 防护你看这个就够了 - 下篇
- 如何最简洁的使用iOS 开发证书 和 Profile 文件
- Hbuilder用自有证书打包 ios App上架AppStore流程
- iOS开发之自定义字体
- 求助 iOS 分发的最佳实践
- IOS 设置百度地图自定义标注图片,自定义泡泡详解手机开发
- iOS 自定义瀑布流相册控件详解手机开发
- iOS 验证码随机出现的六位数详解手机开发
- iOS WebDriverAgent 环境搭建详解手机开发
- Linux下开发iOS应用:挑战还是机会?(linux做ios开发)
- 谷歌服软:终于为Gmail iOS版在App Store里面添加了隐私标签
- 基于Linux环境下iOS应用开发实践(linux下开发ios)
- iOS 不是 Linux,而是苹果独特操作系统(ios是linux吗)
- Android应当从iOS处窃取的五大最佳功能
- IOS开发之路--C语言预处理