iOS中 自定义cell升级版 (高级)
来源:互联网 发布:java 八皇后算法 编辑:程序博客网 时间:2024/06/04 19:58
接上次分享的自定义cell进行了优化:http://blog.csdn.net/qq_31810357/article/details/49611255
指定根视图:
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{ 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; }}
点击cell会走的方法
- (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];}
自定义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;@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 = 44 * 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;}
准备一个model类
//.h#import <Foundation/Foundation.h>@interface WGModel : NSObject@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 新浪微博
1 0
- iOS中 自定义cell升级版 (高级)
- iOS中 自定义cell升级版 (高级)
- IOS中Cell自定义
- ios中自定义cell,自定义UITableViewCell
- ios中自定义table view cell
- iOS tableview cell 中自定义lable
- iOS cell自适应 自定义cell
- iOS 自定义cell
- ios UITableview自定义cell
- IOS学习 Cell自定义
- ios 自定义cell demo
- iOS UI09_自定义cell
- IOS - 自定义cell
- iOS--xib自定义cell
- iOS ---xib自定义cell
- ios 开发自定义cell
- iOS-cell.selectedBackgroundView自定义
- IOS之自定义Cell
- sql中详解round(),floor(),ceiling()函数的用法和区别
- Objective-C中的Block
- iOS8升级IOS 9 和 XCode 7 引起的问题记录
- 常见的一些宏定义
- Debian装的太辛苦了
- iOS中 自定义cell升级版 (高级)
- Flask Web开发
- 有 1 、 2 、 3 、 4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
- 关于Servlet安全策略和GlassFish中的相关设置
- Objective-C:类别&代理
- .NET跨平台初体验
- 在struts2框架下做下载功能
- 第二周学习Python
- linux vim 配置文件(高亮+自动缩进+行号+折叠+优化)