簡單實現(xiàn)類似微信的展開全文功能

??????,廢不多說,直接上效果圖

未命名.gif

然后廢話不多說,直接丟代碼(才不說是因為沒啥亮點,才不裝x的呢??)。

//
//  ViewController.m
//  StretchTableView
//
//  Created by wuwj on 16/9/2.
//  Copyright ? 2016年 wuwj. All rights reserved.
//

#import "ViewController.h"
#import "fiveTableViewCell.h"
#import "UILabel+StringFrame.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;

@end

@implementation ViewController{
    CGFloat height;
    NSString *detailStr;
}
static NSString *fiveCell = @"fivecell";

- (void)viewDidLoad {
    [super viewDidLoad];
    detailStr = [self titleArray][0];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    _tableView = tableView;
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([fiveTableViewCell class]) bundle:nil] forCellReuseIdentifier:fiveCell];
    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return height + 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    fiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fiveCell];
    UILabel *detailLabel = [cell.contentView viewWithTag:11];
    UIButton *button = [cell.contentView viewWithTag:12];
    detailLabel.text = detailStr;
    detailLabel.numberOfLines = 0;
    height = [detailLabel boundingRectWithSize:(CGSizeMake(_tableView.frame.size.width, 1000))].height;
    [button addTarget:self action:@selector(moreMessage) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}

- (void)moreMessage{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    fiveTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:fiveCell forIndexPath:indexPath];
    UIButton *button = [cell.contentView viewWithTag:12];
    if ([button.titleLabel.text isEqualToString:@"收起"]) {
        detailStr =  [self titleArray][0];
        [button setTitle:@"展開全文" forState:(UIControlStateNormal)];
    }else{
        detailStr = [self titleArray][1];
        [button setTitle:@"收起" forState:(UIControlStateNormal)];
    }
    [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:(UITableViewRowAnimationNone)];
    
}

- (NSArray *)titleArray{
    return @[@"9月7日(北京時間9月8日凌晨),蘋果將在舊金山舉行新品發(fā)布會,正式發(fā)布新一代iPhone。隨著發(fā)布日期的臨近,新一代iPhone已經(jīng)曝光的差不多了,不過命名卻讓人有點摸不到頭腦。迄今為止,已經(jīng)有了iPhone 7、iPhone 6 SE以及iPhone 7 SE等多種命名方式。",
             @"9月7日(北京時間9月8日凌晨),蘋果將在舊金山舉行新品發(fā)布會,正式發(fā)布新一代iPhone。隨著發(fā)布日期的臨近,新一代iPhone已經(jīng)曝光的差不多了,不過命名卻讓人有點摸不到頭腦。迄今為止,已經(jīng)有了iPhone 7、iPhone 6 SE以及iPhone 7 SE等多種命名方式。但,最新曝光的包裝盒顯示,新一代iPhone的命名就叫iPhone 7,不會搞出什么幺蛾子了。從外媒曝光的包裝盒來看,iPhone 7 Plus會有32GB版可選,而且包裝盒內附贈一款名為AirPods的無線耳機。此外,此前曝光的消息顯示,包裝盒內可能還會有一個Lightning接口到3.5mm耳機接口轉換器。是不是真的?下周見分曉。"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

跑路(裝完就跑,真爽????)。
https://pan.baidu.com/s/1nuWuPrb百度網(wǎng)盤下載demo

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,422評論 25 708
  • 180文案營結束了,思想上一下子放松了下來,拉了兩篇日志,昨天上后臺一看,果然掉粉了。 都知道堅持不容易,回頭看看...
    勵志寶媽閱讀 612評論 0 0
  • 書到用時方恨少,每每臨時抱佛腳。 早知冬寒碌饑腸,惜挽秋高忘冬糧。 春去秋來百花殘,嚴冬曝夏柳葉彎。 昨日庭前羞看...
    十九郎閱讀 613評論 0 1
  • 我是一個鄉(xiāng)野長大的孩子,為什么是鄉(xiāng)野不是鄉(xiāng)村呢?因為那不算是村,我們是一家人住在一個山頭,周圍很遙遠的地方才有一家...
    出陳記閱讀 878評論 0 0