美文网首页
iOS随笔小记--点击row隐藏或显示内容

iOS随笔小记--点击row隐藏或显示内容

作者: 七一小月 | 来源:发表于2018-05-31 14:37 被阅读22次

    一 、废话不多说,先看效果吧😂 。。。

    1.1 效果图如下:

    显示内容.png 隐藏内容.png
    1.2 文件目录如下: 一个视图控制器,一个自定义cell
    文件目录.png
    1.3 自定义cell的样式如下
    自定义cell的样式.png

    二 、代码部分

    2.1 protocol.m文件的内容

    2.1.1 先定义两个数组
    static NSString * identifier = @"protocolCell";
    @interface protocolVC ()<UITableViewDelegate,UITableViewDataSource>
    {
          NSArray * headerViewTitleArray ;  //存放隐形内容时的title的内容
          NSArray * dataArray;              //存放显示内容时的content数据
    }
     //dataDic记录点击的row是打开还是隐藏内容的
    @property (strong, nonatomic) NSMutableDictionary * dataDic; 
    
    2.1.2 在viewDidLoad里面进行初始化
    [self.protocolTableView registerNib:[UINib nibWithNibName:@"protocolTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    self.protocolTableView.tableFooterView= [[UIView alloc] initWithFrame:CGRectZero];
    self.dataDic = [NSMutableDictionary dictionary];
    
    //因为语言国际化,所以我是用的是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代理方法的实现
    #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文件的内容

    2.2.1 定义从xib拉的控件和初始化方法
    定义从xib拉的控件和初始化方法.png

    2.3 protocolTableViewCell.m文件的内容

    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;
    }
    

    ============OK============

    大功告成啦~~~~~

    相关文章

      网友评论

          本文标题:iOS随笔小记--点击row隐藏或显示内容

          本文链接:https://www.haomeiwen.com/subject/cdkqsftx.html