美文网首页iOS DeveloperiOS技术中心iOS开发技术分享
简单实现类似微信的展开全文功能

简单实现类似微信的展开全文功能

作者: 静花寒 | 来源:发表于2016-09-02 11:35 被阅读826次

😄😄😄,废不多说,直接上效果图

未命名.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日凌晨),苹果将在旧金山举行新品发布会,正式发布新一代iPhone。随着发布日期的临近,新一代iPhone已经曝光的差不多了,不过命名却让人有点摸不到头脑。迄今为止,已经有了iPhone 7、iPhone 6 SE以及iPhone 7 SE等多种命名方式。",
             @"9月7日(北京时间9月8日凌晨),苹果将在旧金山举行新品发布会,正式发布新一代iPhone。随着发布日期的临近,新一代iPhone已经曝光的差不多了,不过命名却让人有点摸不到头脑。迄今为止,已经有了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百度网盘下载demo

相关文章

网友评论

    本文标题:简单实现类似微信的展开全文功能

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