一 悲没、廢話不多說,先看效果吧?? 形娇。锰霜。。
1.1 效果圖如下:
顯示內(nèi)容.png
隱藏內(nèi)容.png
1.2 文件目錄如下: 一個(gè)視圖控制器桐早,一個(gè)自定義cell
文件目錄.png
1.3 自定義cell的樣式如下
自定義cell的樣式.png
二 癣缅、代碼部分
2.1 protocol.m文件的內(nèi)容
2.1.1 先定義兩個(gè)數(shù)組
static NSString * identifier = @"protocolCell";
@interface protocolVC ()<UITableViewDelegate,UITableViewDataSource>
{
NSArray * headerViewTitleArray ; //存放隱形內(nèi)容時(shí)的title的內(nèi)容
NSArray * dataArray; //存放顯示內(nèi)容時(shí)的content數(shù)據(jù)
}
//dataDic記錄點(diǎn)擊的row是打開還是隱藏內(nèi)容的
@property (strong, nonatomic) NSMutableDictionary * dataDic;
2.1.2 在viewDidLoad里面進(jìn)行初始化
[self.protocolTableView registerNib:[UINib nibWithNibName:@"protocolTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
self.protocolTableView.tableFooterView= [[UIView alloc] initWithFrame:CGRectZero];
self.dataDic = [NSMutableDictionary dictionary];
//因?yàn)檎Z言國(guó)際化,所以我是用的是NSLocalizedString(@"termsTitleOne", nil)這種格式
if ([self.protocolName isEqualToString:NSLocalizedString(@"Terms & Conditions", nil)]) {
headerViewTitleArray = @[NSLocalizedString(@"termsTitleOne", nil), NSLocalizedString(@"termsTitleTwo", nil),NSLocalizedString(@"termsTitleThree", nil),NSLocalizedString(@"termsTitleFour", nil),NSLocalizedString(@"termsTitleFive", nil),NSLocalizedString(@"termsTitleSix", nil),NSLocalizedString(@"termsTitleSeven", nil), NSLocalizedString(@"termsTitleEight", nil),NSLocalizedString(@"termsTitleNine", nil),NSLocalizedString(@"termsTitleTen", nil),NSLocalizedString(@"termsTitleEleven", nil),NSLocalizedString(@"termsTitleTwelve", nil)];
dataArray = @[NSLocalizedString(@"termsOne", nil), NSLocalizedString(@"termsTwo", nil),NSLocalizedString(@"termsThree", nil),NSLocalizedString(@"termsFour", nil),NSLocalizedString(@"termsFive", nil),NSLocalizedString(@"termsSix", nil),NSLocalizedString(@"termsSeven", nil), NSLocalizedString(@"termsEight", nil),NSLocalizedString(@"termsNine", nil),NSLocalizedString(@"termsTen", nil),NSLocalizedString(@"termsEleven", nil),NSLocalizedString(@"termsTwelve", nil)];
} else if ([self.protocolName isEqualToString:NSLocalizedString(@"Privacy Notice", nil)]){
headerViewTitleArray = @[NSLocalizedString(@"privacyTitleOne", nil), NSLocalizedString(@"privacyTitleTwo", nil),NSLocalizedString(@"privacyTitleThree", nil),NSLocalizedString(@"privacyTitleFour", nil),NSLocalizedString(@"privacyTitleFive", nil),NSLocalizedString(@"privacyTitleSix", nil),NSLocalizedString(@"privacyTitleSeven", nil), NSLocalizedString(@"privacyTitleEight", nil),NSLocalizedString(@"privacyTitleNine", nil),NSLocalizedString(@"privacyTitleTen", nil),NSLocalizedString(@"privacyTitleEleven", nil),NSLocalizedString(@"privacyTitleTwelve", nil)];
dataArray = @[NSLocalizedString(@"privacyOne", nil), NSLocalizedString(@"privacyTwo", nil),NSLocalizedString(@"privacyThree", nil),NSLocalizedString(@"privacyFour", nil),NSLocalizedString(@"privacyFive", nil),NSLocalizedString(@"privacySix", nil),NSLocalizedString(@"privacySeven", nil), NSLocalizedString(@"privacyEight", nil),NSLocalizedString(@"privacyNine", nil),NSLocalizedString(@"privacyTen", nil),NSLocalizedString(@"privacyEleven", nil),NSLocalizedString(@"privacyTwelve", nil)];
}
2.1.3 TableView代理方法的實(shí)現(xiàn)
#pragma mark -- UIWebViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
protocolTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%zd",indexPath.row]] isEqualToString:@"0"]){
[cell.protocoTitleButton setTitle:@"-" forState:UIControlStateNormal];
[cell setTitleValueForCell:headerViewTitleArray[indexPath.row] andContent:dataArray[indexPath.row]];
} else {
[cell.protocoTitleButton setTitle:@"+" forState:UIControlStateNormal];
[cell setTitleValueForCell:headerViewTitleArray[indexPath.row] andContent:nil];
}
cell.protocoTitleButton.tag = indexPath.row + 1000;
[cell.protocoTitleButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)buttonAction:(UIButton *)sender{
NSInteger temp = sender.tag - 1000;
// temp === 》section
if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%zd",temp]] isEqualToString:@"0"] ){
[self.dataDic setObject:@"1" forKey:[NSString stringWithFormat:@"%zd",temp]];
} else {
[self.dataDic setObject:@"0" forKey:[NSString stringWithFormat:@"%zd",temp]];
}
// reload section
[self.protocolTableView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%zd",indexPath.row]] isEqualToString:@"0"]){
return [self getHeiahtBydescritonStr:headerViewTitleArray[indexPath.row] andFont:k_Helvetica_16 andWidth:85] + [self getHeiahtBydescritonStr:dataArray[indexPath.row] andFont:k_Helvetica_14 andWidth:55] + 50;
} else {
return [self getHeiahtBydescritonStr:headerViewTitleArray[indexPath.row] andFont:k_Helvetica_16 andWidth:85] + 40;
}
}
- (CGFloat)getHeiahtBydescritonStr:(NSString *)descritonStr andFont:(UIFont *)font andWidth:(CGFloat)width{
CGRect frame = [descritonStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil] ;
return frame.size.height;
}
2.2 protocolTableViewCell.h文件的內(nèi)容
2.2.1 定義從xib拉的控件和初始化方法
定義從xib拉的控件和初始化方法.png
2.3 protocolTableViewCell.m文件的內(nèi)容
2.3.1 初始化方法
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.protocoBackView.layer.borderColor = k_fontColor.CGColor;
self.protocoBackView.layer.borderWidth = 0.5;
}
-(void) setTitleValueForCell:(NSString *)title andContent:(NSString *)content {
self.heightOfTitleLabel.constant = [self getHeiahtBydescritonStr:title andFont:k_Helvetica_16 andWidth:85];
if (![content isEqualToString:@""]) {
self.protocoContentLabel.hidden = NO;
self.heightOfContentLabel.constant = [self getHeiahtBydescritonStr:content andFont:k_Helvetica_14 andWidth:55]+ 20;
} else {
self.protocoContentLabel.hidden = YES;
}
self.protocoTitleLabel.font = k_Helvetica_16;
self.protocoContentLabel.font = k_Helvetica_14;
self.protocoTitleLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
self.protocoContentLabel.textColor = k_protocol_color;
self.protocoTitleLabel.text = title;
self.protocoContentLabel.text = content;
}
- (CGFloat) getHeiahtBydescritonStr:(NSString *)descritonStr andFont:(UIFont *)font andWidth:(CGFloat)width{
CGRect frame = [descritonStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil] ;
return frame.size.height;
}
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}