先看tableView中代码
//
// ViewController.m
// JActivityList
//
// Created by juanmao on 15/12/6.
// Copyright © 2015年 juanmao. All rights reserved.
//
#import "ViewController.h"
#import "TextEntity.h"
#import "TextListCell.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property(nonatomic, strong)UITableView *mTabelView;
@property(nonatomic, strong)NSMutableArray *dataArr;
@property(nonatomic,assign) BOOL flag;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"举个";
self.dataArr = [NSMutableArray array];
[self initData];
[self addSubView];
}
- (void)addSubView
{
self.mTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kWidth,kHeight) style:UITableViewStylePlain];
self.mTabelView.delegate = self;
self.mTabelView.dataSource = self;
self.mTabelView.tableFooterView = [UIView new];
[self.view addSubview:self.mTabelView];
}
/*!
* 解析本地json数据
*/
- (void)initData
{
NSString *path=[[NSBundle mainBundle] pathForResource:@"TextInfo" ofType:@"json"];
NSString *jsonContent=[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
if (jsonContent != nil)
{
NSData *jsonData = [jsonContent dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
NSArray *textList = [dic objectForKey:@"textList"];
for (NSDictionary *dict in textList)
{
TextEntity *entity = [[TextEntity alloc]initWithDict:dict];
if (entity)
{
[self.dataArr addObject:entity];
}
}
if(err)
{
NSLog(@"json解析失败:%@",err);
}
}
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
TextListCell *cell = (TextListCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell){
cell = [[TextListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if ([self.dataArr count] > indexPath.row){
//这里的判断是为了防止数组越界
cell.entity = [self.dataArr objectAtIndex:indexPath.row];
}
//自定义cell的回调,获取要展开/收起的cell。刷新点击的cell
cell.showMoreTextBlock = ^(UITableViewCell *currentCell){
[self.mTabelView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
};
return cell;
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
TextEntity *entity = nil;
if ([self.dataArr count] > indexPath.row)
{
entity = [self.dataArr objectAtIndex:indexPath.row];
}
//根据isShowMoreText属性判断cell的高度
if (entity.isShowMoreText)
{
return [TextListCell cellMoreHeight:entity];
}
else
{
return [TextListCell cellDefaultHeight:entity];
}
return 0;
}
@end
核心代码是 点了cell上的按钮后重新刷新改行cell,并重新计算cell高度
[self.mTabelView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
以下是cell中的代码
//
// TextListCell.m
// JActivityList
//
// Created by juanmao on 15/12/7.
// Copyright © 2015年 juanmao. All rights reserved.
//
#define kWidth [UIScreen mainScreen].bounds.size.width
#import "TextListCell.h"
@interface TextListCell()
{
UILabel *_textTitle;
UILabel *_textContent;
UIButton *_moreTextBtn;
}
@end
@implementation TextListCell
+ (CGFloat)cellDefaultHeight:(TextEntity *)entity
{
//默认cell高度
return 85.0;
}
+ (CGFloat)cellMoreHeight:(TextEntity *)entity
{
//展开后得高度(计算出文本内容的高度+固定控件的高度)
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:16]};
NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);
CGSize size = [entity.textContent boundingRectWithSize:CGSizeMake(kWidth - 30, 100000) options:option attributes:attribute context:nil].size;;
return size.height + 50;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_textTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, 140, 20)];
_textTitle.textColor = [UIColor blackColor];
_textTitle.font = [UIFont systemFontOfSize:18];
[self.contentView addSubview:_textTitle];
_textContent = [[UILabel alloc]initWithFrame:CGRectMake(15, 30,kWidth - 30 , 20)];
_textContent.textColor = [UIColor blackColor];
_textContent.font = [UIFont systemFontOfSize:16];
_textContent.numberOfLines = 0;
[self.contentView addSubview:_textContent];
_moreTextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_moreTextBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
_moreTextBtn.frame = CGRectMake(kWidth - 50, 5, 40, 20);
[self.contentView addSubview:_moreTextBtn];
[_moreTextBtn addTarget:self action:@selector(showMoreText) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
_textTitle.text = self.entity.textName;
_textContent.text = self.entity.textContent;
if (self.entity.isShowMoreText)
{
///计算文本高度
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:16]};
NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);
CGSize size = [self.entity.textContent boundingRectWithSize:CGSizeMake(kWidth - 30, 100000) options:option attributes:attribute context:nil].size;
[_textContent setFrame:CGRectMake(15, 30, kWidth - 30, size.height)];
[_moreTextBtn setTitle:@"收起" forState:UIControlStateNormal];
}
else
{
[_moreTextBtn setTitle:@"展开" forState:UIControlStateNormal];
[_textContent setFrame:CGRectMake(15, 30, kWidth - 30, 35)];
}
}
- (void)showMoreText
{
//将当前对象的isShowMoreText属性设为相反值
self.entity.isShowMoreText = !self.entity.isShowMoreText;
if (self.showMoreTextBlock)
{
self.showMoreTextBlock(self);
}
}
@end
重点是计算高度部分
+ (CGFloat)cellMoreHeight:(TextEntity *)entity
{
//展开后得高度(计算出文本内容的高度+固定控件的高度)
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:16]};
NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);
CGSize size = [entity.textContent boundingRectWithSize:CGSizeMake(kWidth - 30, 100000) options:option attributes:attribute context:nil].size;;
return size.height + 50;
}