iOS -點(diǎn)擊row隱藏或顯示內(nèi)容

一 悲没、廢話不多說,先看效果吧?? 形娇。锰霜。。

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

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

大功告成啦~~~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末哄酝,一起剝皮案震驚了整個(gè)濱河市友存,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌陶衅,老刑警劉巖爬立,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異万哪,居然都是意外死亡侠驯,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門奕巍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吟策,“玉大人,你說我怎么就攤上這事的止¢菁幔” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵诅福,是天一觀的道長(zhǎng)匾委。 經(jīng)常有香客問我,道長(zhǎng)氓润,這世上最難降的妖魔是什么赂乐? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮咖气,結(jié)果婚禮上挨措,老公的妹妹穿的比我還像新娘挖滤。我一直安慰自己,他們只是感情好浅役,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布斩松。 她就那樣靜靜地躺著,像睡著了一般觉既。 火紅的嫁衣襯著肌膚如雪惧盹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天瞪讼,我揣著相機(jī)與錄音钧椰,去河邊找鬼。 笑死尝艘,一個(gè)胖子當(dāng)著我的面吹牛演侯,可吹牛的內(nèi)容都是我干的姿染。 我是一名探鬼主播背亥,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼悬赏!你這毒婦竟也來了狡汉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤闽颇,失蹤者是張志新(化名)和其女友劉穎盾戴,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體兵多,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡尖啡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了剩膘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衅斩。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖怠褐,靈堂內(nèi)的尸體忽然破棺而出畏梆,到底是詐尸還是另有隱情,我是刑警寧澤奈懒,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布奠涌,位于F島的核電站,受9級(jí)特大地震影響磷杏,放射性物質(zhì)發(fā)生泄漏溜畅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一极祸、第九天 我趴在偏房一處隱蔽的房頂上張望达皿。 院中可真熱鬧天吓,春花似錦、人聲如沸峦椰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汤功。三九已至物邑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間滔金,已是汗流浹背色解。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留餐茵,地道東北人科阎。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像忿族,于是被迫代替她去往敵國(guó)和親锣笨。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容