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

指定根视图:

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

RootTableViewController.m

  1. #import "WGModel.h"
  2. #import "WGCell.h"
  3.  
  4. @interface RootTableViewController ()
  5.  
  6. @property (nonatomic, strong) NSMutableDictionary *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. }

请求数据:

  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. }

#pragma mark - Table view data source

  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. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  13. {
  14. WGCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  15.  
  16. NSString *key = self.dataDict.allKeys[indexPath.section];
  17. NSMutableArray *mutableArray = self.dataDict[key];
  18. WGModel *model = mutableArray[indexPath.row];
  19. [cell configureCellWithModel:model];
  20.  
  21. if (model.isShow == YES) {
  22. [cell showTableView];
  23. } else {
  24.  
  25. [cell hiddenTableView];
  26. }
  27.  
  28. return cell;
  29. }

自适应高

  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会走的方法

  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

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

准备一个model类

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

最终效果:

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

iOS中 自定义cell升级版 (高级)的更多相关文章

  1. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  2. iOS中 自定义cell分割线/分割线偏移 韩俊强的博客

    在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考: 每日更新关注 ...

  3. iOS 中自定义 cell,点击cell的时候文字不出现的原因

    解决方案: 在setSelected方法中设置要显示label的背景颜色即可

  4. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  5. ios之UI中自定义cell

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  6. iOS中 UITableViewCell cell划线那些事 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang 在开发中经常遇到cell分割线显示不全或者想自定义线的宽高等; 最近总结了一下,希望帮到大家: 1.不想划线怎么办? Table ...

  7. ios中自定义checkbox

    //自定义button#import <UIKit/UIKit.h> @interface CKButton : UIButton @end #import "CKButton. ...

  8. iOS 获取自定义cell上按钮所对应cell的indexPath.row的方法

    在UITableView或UICollectionView的自定义cell中创建一button,在点击该按钮时知道该按钮所在的cell在UITableView或UICollectionView中的行数 ...

  9. iOS中自定义UITableViewCell的用法

    1.先创建一个View继承 UITableViewCell并使用xib快速建立模型. #import <UIKit/UIKit.h> #import "Score.h" ...

随机推荐

  1. 解决Spring Boot 使用RedisTemplate 存储键值出现乱码 \xac\xed\x00\x05t\x00

    spring-data-redis的RedisTemplate<K, V>模板类在操作redis时默认使用JdkSerializationRedisSerializer来进行序列化解决方法 ...

  2. Apache软件基金会项目百度百科链接

    Apache软件基金会 顶级项目 ▪ ActiveMQ ▪ Ant ▪ Apache HTTP Server ▪ APR ▪ Beehive ▪ Camel ▪ Cassandra ▪ Cayenne ...

  3. 使用Fiddler改变线上js文件的引用路径

    一般的项目开发都是先在本地环境开发,测试环境中完成测试,最后再提交到线上环境. 但是由于版本构建工具有时出现bug或者一些缓存的因素导致测试环境代码可能和线上不一样,这是多么蓝瘦的事情.此处说的是在原 ...

  4. 深入理解Lambda函数及其用法

    Lambda函数又称匿名函数,匿名函数就是没有名字的函数,函数没有名字也行?当然可以啦.有些函数如果只是临时一用,而且它的业务逻辑也很简单时,就没必要非给它取个名字不可. 先来看个简单lambda函数 ...

  5. 网络流24题第一题(luogu2796飞行员配对方案)

    飞行员配对方案 二分图裸题,可以拿最大流怼. 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 ...

  6. 将luarocks整合进openresty

    缘由 随着功能需求的深入, openresty领域的包已经不够用了, 需要lua领域本身累积的库, 也就是luarocks. 本文讲解了windows 10桌面和ubuntu server两套系统的方 ...

  7. ROS新功能包PlotJuggler绘图

    http://www.ros.org/news/2017/01/new-package-plotjuggler.html PlotJuggler,一个基于Qt的应用程序,允许用户加载,搜索和绘图数据. ...

  8. Appium移动自动化测试(五)--app控件获取之uiautomatorviewer

    初探 在Android的SDk提供了以下的工具来支持我们进行UI自动化测试: uiautomatorviewer:用来扫描和分析Android应用程序的UI控件的工具. uiautomator:一个包 ...

  9. 输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。

    输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. #define N 15 void findS ...

  10. 详解EBS接口开发之采购订单导入

    采购订单常用标准表简介 1.1   常用标准表 如下表中列出了与采购订单导入相关的表和说明: 表名 说明 其他信息 po.po_headers_all 采购订单头 采购订单号,采购类型,供应商,地点, ...