ios-uitableviewcell展开
#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展开的更多相关文章
- iOS UITableViewCell UITableVIewController 纯代码开发
iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...
- iOS UITableViewCell的"滑动出现多个按钮"
本文授权转载,作者:@夏天是个大人了 前言: 本篇博客其实就是想介绍tableviewcell滑动的一些"事",昨天在逛github的时候看到的还挺有意思的三方库,简单用了一下感觉 ...
- iOS UITableViewCell点击时子视图背景透明的解决方法
在做iOS项目的开发中,UITableView控件的应用十分广泛.在进行自定义UITableViewCell时,经常遇到这样的问题:在UITableViewCell上面添加了一个有背景颜色的子视图,当 ...
- ios UITableViewCell重用问题
在写sina 微博界面的过程中使用到了cell,那么就是在cell上添加一些控件,但是由于每条微博的内容都是不同的,所以在显示的过程中,出现了内容重叠的问题,其实就是UITableViewCell重用 ...
- iOS UITableViewCell滑动删除
一般我们使用列表的形式展现数据就会用到UITableView.在熟练掌握了用UITableView展示数据以后,开发过程中可能会遇到需要删除数据的需求,我们想实现在一行数据上划动一下,然后出现一个删除 ...
- iOS UITableViewCell的分割线向左延长15(cell长度为全宽)
iOS7情况下: tableView.separatorInset = UIEdgeInsetsZero; iOS8.9情况下: 首先在viewDidLoad方法中加上如下代码: if ([table ...
- IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...
- iOS UITableViewCell 中 调整imageView 的图片大小
在我的项目中,很多地方都希望将UITableViewCell 中的imageView 能根据自己图片的大小来进行展示,而就为了解决这个问题又觉得重写UITableViewCell 很不值得. 如下: ...
- ios uitableviewcell动态计算高度
#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...
- 点击UITableviewCell展开收缩
#import "ViewController.h" #import "ZSDTestCell.h" @interface ViewController ()& ...
随机推荐
- 前端异步解决方案——mmDeferred
Deferred是前端解决异步操作的一种编程范式,后来出现的Promise规范更是让其普适性大大提高.不过Promise规范也存在分岐.现在最流行的是Promise/A规范. Promise/A大致是 ...
- C++ 中vector的基本用法
//在网上看了好久,自己总结了一下下,第一篇博客,呼呼,学到不少 基本概念 vector容器是一个模板类,可以存放任何类型的对象).vector对象可以在运行时高效地添加元素,并且vector中元素是 ...
- TdxAlertWindowManager右下角HINT显示控件
带爱像的右下角HINT显示,自动隐藏 function alterInfo: TdxAlertWindowManager;begin if not Assigned(Falter) then be ...
- oracle学习 十二 使用.net程序调用带返回值的存储过程(持续更新)
数据库返回的是结果集,存储过程返回的是一个或者多个值,所以不要使用while循环去读取,也不要使用datareader函数去调用.v_class_name是返回函数 使用.net调用oracle数据库 ...
- [转]前景检测算法--ViBe算法
原文:http://blog.csdn.net/zouxy09/article/details/9622285 转自:http://blog.csdn.net/app_12062011/article ...
- 1001.A+B Format (20)(思路,bug发现及其修改,提交记录)
https://github.com/031502316a/object-oriented/tree/master/1001 ---恢复内容开始--- 1.解题思路 一开始见到题目时,感觉难的就是输出 ...
- UVa 1620 Lazy Susan (找规律)
题意:给 n 个数,每次可以把4个连续的数字翻转,问你能不能形成1-n的环状排列. 析:找一下奇偶性,写几个数试试,就会找到规律. 代码如下: #include <cstdio> #inc ...
- jsp两种包含方式
jsp中存在两种文件的包含指令 1.<%@include file="xxx.jsp" %> 2.<jsp:include page="xxx.jsp& ...
- [网络]远程访问局域网svn服务器[转]
转至:http://8474832.blog.51cto.com/8464832/1555449 打开路由器访问界面 选择转发规则->端口映射-新建 在弹出的界面中填写相应的端口号了内网ip 填 ...
- 关于html和body高度 登录页面登录框永远保持在页面垂直水平居中位置
HTML CSS 解说,水平居中就不用多说了,给div.login_wrap设置个宽度,然后margin:0 auto;就可以做到.但是这里这样做,这里使用了两层div 外层使用margin:0 au ...