接上次分享的自定义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 
新浪微博

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. DCOM EXCE权限配置问题

    检索COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误: 80070005: 关于以上这个问题,博主在百度上 ...

  2. Python里面 search0和 match0的区别?

    这是正则表达式里面的函数: match()函数只检测RE是不是在string的开始位置匹配,search()会扫描整个string查找匹配: 也就是说match()只有在0位置匹配成功的话才有返回,如 ...

  3. JVM常见问题 一(转载)

    1. 内存模型以及分区 JVM内存模型如下图所示: 此处我们集中注意中间绿色的部分,该部分为JVM的运行时内存,该部分包含了: 线程私有的(灰色): 程序计数器:记录执行到第几条指令 虚拟机方法栈:执 ...

  4. 项目中BigDecimal与Double使用场景

    金额要用BigDecimal 金额计算不能用doube!!!! 金额计算必须用BigDecimal,下面对比一下用double 跟BigDecimal的区别.先看一个小例子: 请看题: 示例1 问, ...

  5. AsyncLocal 与 async await

    大家来看一张图 先猜猜看为什么会这样 关于async await的原理 建议查看 https://blog.csdn.net/brook_shi/article/details/50803957 这篇 ...

  6. 深入理解Oracle中的随机函数

    --Oracle中取随机值的函数 .dbms_random包 dbms_random包提供了一系列的随机值获取函数以及相关存储过程.下面详细讲解常用的函数和过程. ()dbms_random.rand ...

  7. 下篇:python的基本数据类型以及对应的常用方法(列表、元组、字典、集合)

    为了日后便于查询,本文所涉及到的所有命令集合如下: python中的基本数据类型有数字.字符串.布尔值.列表.元组.字典.就像每一个职业有自己特定的技能,比如医生能看病,农民能种田,每种数据类型也有属 ...

  8. SpringMVC之拦截器实现登录验证

    今天回头看之前发的javaweb学习路线图,发现把路线图中的也学的有一半多了,不过还是路漫漫.在前面的博客中有学习过spring的aop,它利用动态代理实现,在springmvc中也是一样,今天使用H ...

  9. Docker新手入门:基本用法

    Docker新手入门:基本用法 1.Docker简介 1.1 第一本Docker书 工作中不断碰到Docker,今天终于算是正式开始学习了.在挑选系统学习Docker以及虚拟化技术的书籍时还碰到了不少 ...

  10. AndroidStudio中导入SlidingMenu报错解决方案

    ----------------------------------------------------------------------------------------------[版权申明: ...