首页 > 代码库 > ios-uitableviewcell展开
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展开