#import <UIKit/UIKit.h>

@interface ZSDHelpCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *selectImageView;

@property(nonatomic,copy)NSString *question;

@property(nonatomic,copy)NSString *answer;

//获取展开后的高度

-(CGFloat)getExpandHeight;

@end

#import "ZSDHelpCell.h"

//判断系统版本

#define IOS_VERSION_7_OR_ABOVE (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)? (YES):(NO))

@interface ZSDHelpCell()

@property (weak, nonatomic) IBOutlet UILabel *questionLabel;

@property (weak, nonatomic) IBOutlet UILabel *answerLabel;

@end

@implementation ZSDHelpCell

- (void)awakeFromNib

{

// Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

[super setSelected:selected animated:animated];

// Configure the view for the selected state

}

-(void)setAnswer:(NSString *)answer

{

if (_answer!=answer)

{

_answer=answer;

_answerLabel.text=_answer;

}

}

-(void)setQuestion:(NSString *)question

{

if (_question!=question)

{

_question=question;

_questionLabel.text=_question;

}

}

-(CGFloat)getExpandHeight

{

//ios8对于systemLayoutSizeFittingSize这个方法有效

return IOS_VERSION_7_OR_ABOVE==1?[self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height:120;

}

@end

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

#import "ViewController.h"

#import "ZSDHelpCell.h"

#define kDefaultHeight 44.0f

#define kTabeleHeaderHeight 40.0f

//背景色

#define kThemeBackGroundColor [UIColor colorWithRed:0.93 green:0.92 blue:0.92 alpha:1]

#pragma mark - life circle

@interface ViewController ()

{

NSMutableArray *expandArray;//展开的数组

NSMutableDictionary *cellHeightDic;//设置cell高度的字典

NSMutableDictionary *dataSourceDic;//读取plist文件内容

}

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

_myTableView.backgroundColor=kThemeBackGroundColor;

[self InitializationArrayOrDictionary];

[self loadContentFromPlist];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - private

-(void)InitializationArrayOrDictionary

{

expandArray=[NSMutableArray array];

cellHeightDic=[NSMutableDictionary dictionary];

dataSourceDic=[NSMutableDictionary dictionary];

}

-(void)loadContentFromPlist

{

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"Content" ofType:@"plist"];

dataSourceDic=[NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

//NSLog(@"datasourcedic=%@",[dataSourceDic allValues]);

}

#pragma mark - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return dataSourceDic.count;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

NSArray *sectionArray=[dataSourceDic allValues];

NSDictionary *rowDic=[sectionArray objectAtIndex:section];

return rowDic.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if ([expandArray containsObject:indexPath])

{

NSString *key=[NSString stringWithFormat:@"%ld",indexPath.section];

if ([cellHeightDic objectForKey:key])

{

return [[cellHeightDic objectForKey:key] floatValue];

}

}

return kDefaultHeight;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return kTabeleHeaderHeight;

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

//section添加一个view和label

CGRect tempRect=CGRectMake(0, 0, tableView.bounds.size.width,kTabeleHeaderHeight);

UIView *headerView = [[UIView alloc]initWithFrame:tempRect];

headerView.backgroundColor=kThemeBackGroundColor;

NSArray *sectionArray=[dataSourceDic allKeys];

NSString *text=[sectionArray objectAtIndex:section];

UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(12.0f, 14.0f, 100.0f, 15.0f)];

textLabel.font = [UIFont systemFontOfSize:15.0f];

textLabel.backgroundColor = [UIColor clearColor];

textLabel.textColor = [UIColor grayColor];

textLabel.text = text;

[headerView addSubview:textLabel];

return headerView;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

ZSDHelpCell *cell=(ZSDHelpCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSArray *sectionArray=[dataSourceDic allValues];

NSDictionary *rowDic=[sectionArray objectAtIndex:indexPath.section];

NSArray *questionList=[rowDic allKeys];

NSArray *answerList=[rowDic allValues];

cell.question=[questionList objectAtIndex:indexPath.row];

//设置每个cell的高度key

NSString *key=[NSString stringWithFormat:@"%ld",indexPath.section];

if (![cellHeightDic objectForKey:key])

{

cell.answer=[answerList objectAtIndex:indexPath.row];

CGFloat height=[cell getExpandHeight];

[cellHeightDic setObject:[NSNumber numberWithFloat:height] forKey:key];

}

// 隐藏cell

UIImage *normalImg = [UIImage imageNamed:@"member_icon_more"];

//展开cell

UIImage *selectImg = [UIImage imageNamed:@"common_icon_down"];

if ([expandArray containsObject:indexPath])

{

cell.selectImageView.image=selectImg;

cell.answer=[answerList objectAtIndex:indexPath.row];

}

else

{

cell.selectImageView.image=normalImg;

cell.answer=nil;

}

return cell;

}

#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

//如果数组中不存在该索引,那么把它添加到数组中

if(![expandArray containsObject:indexPath])

{

[expandArray addObject:indexPath];

}

//否则从数组中移除

else

{

[expandArray removeObject:indexPath];

}

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

@end

ios-uitableviewcell展开的更多相关文章

  1. iOS UITableViewCell UITableVIewController 纯代码开发

    iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...

  2. iOS UITableViewCell的"滑动出现多个按钮"

    本文授权转载,作者:@夏天是个大人了 前言: 本篇博客其实就是想介绍tableviewcell滑动的一些"事",昨天在逛github的时候看到的还挺有意思的三方库,简单用了一下感觉 ...

  3. iOS UITableViewCell点击时子视图背景透明的解决方法

    在做iOS项目的开发中,UITableView控件的应用十分广泛.在进行自定义UITableViewCell时,经常遇到这样的问题:在UITableViewCell上面添加了一个有背景颜色的子视图,当 ...

  4. ios UITableViewCell重用问题

    在写sina 微博界面的过程中使用到了cell,那么就是在cell上添加一些控件,但是由于每条微博的内容都是不同的,所以在显示的过程中,出现了内容重叠的问题,其实就是UITableViewCell重用 ...

  5. iOS UITableViewCell滑动删除

    一般我们使用列表的形式展现数据就会用到UITableView.在熟练掌握了用UITableView展示数据以后,开发过程中可能会遇到需要删除数据的需求,我们想实现在一行数据上划动一下,然后出现一个删除 ...

  6. iOS UITableViewCell的分割线向左延长15(cell长度为全宽)

    iOS7情况下: tableView.separatorInset = UIEdgeInsetsZero; iOS8.9情况下: 首先在viewDidLoad方法中加上如下代码: if ([table ...

  7. IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte

    1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...

  8. iOS UITableViewCell 中 调整imageView 的图片大小

    在我的项目中,很多地方都希望将UITableViewCell 中的imageView 能根据自己图片的大小来进行展示,而就为了解决这个问题又觉得重写UITableViewCell 很不值得. 如下: ...

  9. ios uitableviewcell动态计算高度

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...

  10. 点击UITableviewCell展开收缩

    #import "ViewController.h" #import "ZSDTestCell.h" @interface ViewController ()& ...

随机推荐

  1. 【131】如何讲好PPT

    1 列提纲2 写稿子3 背稿子4 演练5遍,用自己的话说出来,最好和稿子一样,但不强求一样,关键要理解5 不一定要做,但是做好了会有很大提高,讲的时候也会很NB:有时间可以再演练几遍,录出来看看哪里需 ...

  2. Skeletal Animation

    [Skeletal Animation] Skeletal animation is the use of “bones” to animate a model. The movement of bo ...

  3. MBR所在位置

    如果offset的000000000位置如下图所示(主要看红色框框位置是否出现NTFS字样),说明系统文件是NTFS "EB 52"至"55 AA"位置是MBR ...

  4. Windows消息机制要点

    1. 窗口过程     每个窗口会有一个称为窗口过程的回调函数(WndProc),它带有四个参数,分别为:窗口句柄(Window Handle),消息ID(Message ID),和两个消息参数(wP ...

  5. chrome emulator use-agent 设置 chrom模拟手机客户端

    谷歌升级以后,发现找不到use-agent设置了 在Element 下点击ESC 出现console,再点击Emulation就出现了

  6. IE=EmulateIE8和IE=IE8的区别

    IE=8<meta http-equiv="X-UA-Compatible" content="IE=8" />This forces IE 8 t ...

  7. 钉钉开发笔记(3)MySQL的配置

    最近在编写web的过程中,经常需要与后台工作人员互动.由于比较麻烦.没有效率. 就果断的请教了,公司的后台大牛,学习下数据库的一些简单操作,现在就把利用MySQL连接服务器, 进行可视化操作的简单步骤 ...

  8. 单步运行linux kernel ?

    如果能给linux kernel打log,如果能单步运行,如果能像普通应用程序那样,step by step的查看程序跑哪里了.. 该多好?这是一个梦想,但从未实现过:因为那是kernel,不是应用程 ...

  9. intval()和(int)转换使用与区别

    <?php echo "<br/>数值强制转换:"; $string="2a"; $string1=intval($string); echo ...

  10. Python在Windows上的安装

    1:下载 http://dlsw.baidu.com/sw-search-sp/soft/6e/17016/python-3.3.5.1395976247.msi 2:直接next安装 3:验证安装是 ...