iOS中 自定义cell升级版 (高级)

简介:

接上次分享的自定义cell进行了优化:http://blog.csdn.net/qq_31810357/article/details/49611255

指定根视图:

[objc]  view plain  copy
  1. self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];  

RootTableViewController.m

[objc]  view plain  copy
  1. #import "WGModel.h"  
  2. #import "WGCell.h"  
  3.   
  4. @interface RootTableViewController ()  
  5.   
  6. @property (nonatomicstrongNSMutableDictionary *dataDict;  
  7.   
  8. @end  
  9.   
  10. @implementation RootTableViewController  
  11.   
  12. - (void)viewDidLoad  
  13. {  
  14.     [super viewDidLoad];  
  15.       
  16.     self.dataDict = [NSMutableDictionary dictionary];  
  17.       
  18.     [self.tableView registerClass:[WGCell class] forCellReuseIdentifier:@"cell"];  
  19.     [self loadDataAndShow];  
  20. }  

请求数据:

[objc]  view plain  copy
  1. - (void)loadDataAndShow  
  2. {  
  3.     NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/trips/2387133727/schedule/"];  
  4.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  5.     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  6.          
  7.         if (data != nil) {  
  8.             NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];  
  9.             for (NSDictionary *dict in array) {  
  10.                 NSString *key = dict[@"date"];  
  11.                 NSArray *placesArray = dict[@"places"];  
  12.                 NSMutableArray *mutableArray = [NSMutableArray array];  
  13.                 for (NSDictionary *placesDict in placesArray) {  
  14.                     WGModel *model = [[WGModel alloc] init];  
  15.                     [model setValuesForKeysWithDictionary:placesDict];  
  16.                     model.isShow = NO;  
  17.                     [mutableArray addObject:model];  
  18.                 }  
  19.                 [self.dataDict setObject:mutableArray forKey:key];  
  20.             }  
  21.             [self.tableView reloadData];  
  22.         }  
  23.           
  24.           
  25.     }];  
  26. }  

#pragma mark - Table view data source

[objc]  view plain  copy
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     return self.dataDict.allKeys.count;  
  4. }  
  5.   
  6. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  7. {  
  8.     NSString *key = self.dataDict.allKeys[section];  
  9.     return [self.dataDict[key] count];  
  10. }  
  11.   
  12.   
  13. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  14. {  
  15.     WGCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];  
  16.       
  17.     NSString *key = self.dataDict.allKeys[indexPath.section];  
  18.     NSMutableArray *mutableArray = self.dataDict[key];  
  19.     WGModel *model = mutableArray[indexPath.row];  
  20.     [cell configureCellWithModel:model];  
  21.       
  22.     if (model.isShow == YES) {  
  23.         [cell showTableView];  
  24.     } else {  
  25.           
  26.         [cell hiddenTableView];  
  27.     }  
  28.       
  29.     return cell;  
  30. }  

自适应高

[objc]  view plain  copy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSString *key = self.dataDict.allKeys[indexPath.section];  
  4.     NSMutableArray *mutableArray = self.dataDict[key];  
  5.     WGModel *model = mutableArray[indexPath.row];  
  6.     if (model.isShow) {  
  7.         return (model.pois.count + 1) * 44;  
  8.     } else {  
  9.         return 44;  
  10.     }  
  11. }  

点击cell会走的方法

[objc]  view plain  copy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSString *key = self.dataDict.allKeys[indexPath.section];  
  4.     NSMutableArray *mutableArray = self.dataDict[key];  
  5.     WGModel *model = mutableArray[indexPath.row];  
  6.     model.isShow = !model.isShow;  
  7.     [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
  8. }  

自定义cell

[objc]  view plain  copy
  1. //.h  
  2. #import <UIKit/UIKit.h>  
  3. @class WGModel;  
  4. @interface WGCell : UITableViewCell<UITableViewDataSource, UITableViewDelegate>  
  5.   
  6. @property (nonatomicstrongUILabel *aLabel;  
  7. @property (nonatomicstrongUITableView *tableView;  
  8.   
  9.   
  10. - (void)configureCellWithModel:(WGModel *)model;  
  11.   
  12. - (void)showTableView;  
  13. - (void)hiddenTableView;  
  14.   
  15. @end  
  16.   
  17. //.m  
  18. #import "WGCell.h"  
  19. #import "WGModel.h"  
  20.   
  21. @interface WGCell ()  
  22.   
  23. @property (nonatomicstrongNSMutableArray *dataArray;  
  24.   
  25. @end  
  26.   
  27. @implementation WGCell  
  28.   
  29.   
  30. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  31. {  
  32.     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {  
  33.         self.dataArray = [NSMutableArray array];  
  34.         [self addAllViews];  
  35.     }  
  36.     return self;  
  37. }  
  38.   
  39. - (void)addAllViews  
  40. {  
  41.     self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake(00, [UIScreen mainScreen].bounds.size.width44)];  
  42.     self.aLabel.backgroundColor = [UIColor greenColor];  
  43.     [self.contentView addSubview:self.aLabel];  
  44.     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(044, [UIScreen mainScreen].bounds.size.width0) style:UITableViewStylePlain];  
  45.       
  46.     self.tableView.delegate = self;  
  47.     self.tableView.dataSource = self;  
  48.     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"testCell"];  
  49. //    [self.contentView addSubview:self.tableView];  
  50. }  
  51.   
  52. - (void)showTableView  
  53. {  
  54.     [self.contentView addSubview:self.tableView];  
  55. }  
  56.   
  57. - (void)hiddenTableView  
  58. {  
  59.     [self.tableView removeFromSuperview];  
  60. }  
  61.   
  62. - (void)configureCellWithModel:(WGModel *)model  
  63. {  
  64.     [self.dataArray removeAllObjects];  
  65.     self.aLabel.text = model.place[@"name"];  
  66.       
  67.     NSArray *array = model.pois;  
  68.     for (NSDictionary *dict in array) {  
  69.         NSString *str = dict[@"name"];  
  70.         [self.dataArray addObject:str];  
  71.     }  
  72.     CGRect frame = self.tableView.frame;  
  73.     frame.size.height = 444 * array.count;  
  74.     self.tableView.frame = frame;  
  75.     [self.tableView reloadData];  
  76.       
  77. }  
  78.   
  79.   
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  81. {  
  82.     return 1;  
  83. }  
  84.   
  85. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  86. {  
  87.     return self.dataArray.count;  
  88. }  
  89.   
  90. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  91. {  
  92.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath];  
  93.     NSString *str = self.dataArray[indexPath.row];  
  94.     cell.textLabel.text = str;  
  95.     return cell;  
  96. }  

准备一个model类

[objc]  view plain  copy
  1. //.h  
  2. #import <Foundation/Foundation.h>  
  3.   
  4. @interface WGModel : NSObject  
  5.   
  6. @property (nonatomic, assign) BOOL isShow;  
  7. @property (nonatomicstrongNSDictionary *place;  
  8. @property (nonatomicstrongNSArray *pois;  
  9.   
  10. @end  
  11.   
  12.   
  13. //.m  
  14. #import "WGModel.h"  
  15.   
  16. @implementation WGModel  
  17.   
  18. - (void)setValue:(id)value forUndefinedKey:(NSString *)key  
  19. {  
  20.       
  21. }  
  22.   
  23. @end  

最终效果:



每日更新关注:http://weibo.com/hanjunqiang  新浪微博


原文地址:http://blog.csdn.net/qq_31810357/article/details/49847711
相关文章
|
7月前
|
设计模式 测试技术 iOS开发
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(1)
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(1)
232 0
|
7月前
|
设计模式 搜索推荐 iOS开发
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(7)
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(7)
503 1
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(7)
|
7月前
|
iOS开发 Perl
iOS Cocoapods 升级
iOS Cocoapods 升级
60 0
|
7月前
|
设计模式 API iOS开发
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(2)
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(2)
269 0
|
7月前
|
设计模式 API iOS开发
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(3)
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(3)
406 0
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(3)
|
7月前
|
设计模式 iOS开发
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(6)
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(6)
247 0
|
2月前
|
移动开发 开发工具 数据安全/隐私保护
iOS APP 版本更新升级教程:如何打包上架新的 APP 版本?
iOS APP 版本更新升级教程:如何打包上架新的 APP 版本?
iOS APP 版本更新升级教程:如何打包上架新的 APP 版本?
|
7月前
|
iOS开发
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
219 0
|
6月前
|
安全 Go 数据安全/隐私保护
免费升级到 iOS 17 Developer Beta:官方Apple Store升级方案与爱思助手方法比较
免费升级到 iOS 17 Developer Beta:官方Apple Store升级方案与爱思助手方法比较
225 0
|
7月前
|
API iOS开发
iOS 自定义转场动画 UIViewControllerTransitioning
iOS 自定义转场动画 UIViewControllerTransitioning
48 0