iOS tableviewcell里点击文字展开与收起功能

2024-01-12 21:38

本文主要是介绍iOS tableviewcell里点击文字展开与收起功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

做项目遇到动态列表中需要做文字展开与收起功能,有空单独抽出一个demo给大家参考。
1、效果图如下:
在这里插入图片描述
在这里插入图片描述

2、创建tableview与添加数据

- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];self.title = @"在tablecell中文本的展开与收起";self.allData = [NSMutableArray array];//添加子视图[self addSubViewUI];//请求数据[self requestMessageListData];//增加刷新[self addRefresh];
}

模拟请求数据

#pragma mark ==========请求数据==========
-(void)requestMessageListData{NSArray *arr = @[ ];for (int i = 0; i<10; i++) {HBTextChangeModel *model = [HBTextChangeModel new];model.content = arr[arc4random_uniform(10)];[self.allData addObject:model];}[self endRefresh];[self.tableView reloadData];
}

设置上拉加载,下拉刷新

- (void)endRefresh{if (self.tableView.mj_header.refreshing) {[self.tableView.mj_header endRefreshing];}if (self.tableView.mj_footer.refreshing) {[self.tableView.mj_footer endRefreshing];}
}
- (void)addRefresh{WEAKSELF;self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{//请求数据// weakSelf.pageNo = 1;[weakSelf.allData removeAllObjects];[weakSelf requestMessageListData];}];self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{//请求数据//weakSelf.pageNo++;[weakSelf requestMessageListData];}];
}

添加子视图

#pragma mark ==========添加子视图==========
-(void)addSubViewUI{[self.view addSubview:self.backBtn];[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(self.view).offset(20);make.left.equalTo(self.view).offset(15);make.size.mas_equalTo(CGSizeMake(100, 25));}];[self.view addSubview:self.tableView];[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(self.backBtn.mas_bottom);make.bottom.equalTo(self.view);make.left.right.equalTo(self.view);}];
}
#pragma mark ==========tableViewDelegate==========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return self.allData.count;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 1;
}#pragma mark ==========tableview代理方法==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{HBTextChangeTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBTextChangeTabCell"];if (cell == nil) {cell = [[HBTextChangeTabCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBTextChangeTabCell"];}cell.selectionStyle = UITableViewCellSelectionStyleNone;cell.backgroundColor = [UIColor clearColor];HBTextChangeModel *model = self.allData[indexPath.section];cell.model = model;WEAKSELF;cell.updateMessageCellBlock = ^(NSString * _Nonnull str) {[weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];};[cell layoutIfNeeded];return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return UITableViewAutomaticDimension;
}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {return 15;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{//全部商品UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];headerView.backgroundColor = [UIColor grayColor];return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{//全部商品UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];footerView.backgroundColor = [UIColor clearColor];return footerView;
}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}#pragma mark ==========tableViewGetter==========
-(UITableView *)tableView{if (!_tableView) {_tableView = ({UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];tableView.dataSource = self;tableView.delegate = self;tableView.backgroundColor = [UIColor clearColor];//设置分割线样式tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//cell的分割线距视图距离tableView.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);//隐藏底部多余分割线tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];//iOS11默认开启Self-Sizing,需关闭才能设置Header,Footer高度tableView.estimatedRowHeight = 66;tableView.estimatedSectionHeaderHeight = 0;tableView.estimatedSectionFooterHeight = 0;tableView.rowHeight = 50 ;tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];;tableView ;}) ;}return _tableView;
}
-(UIButton *)backBtn{if (!_backBtn) {_backBtn = ({//创建按钮UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];//设置标题[button setTitle:@"返回" forState:UIControlStateNormal];//设置字体大小button.titleLabel.font = [UIFont systemFontOfSize:14];//设置title颜色[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//添加点击事件[button addTarget:self action:@selector(clickBackButton:) forControlEvents:UIControlEventTouchUpInside];button;});}return _backBtn;
}
#pragma mark ==========点击返回==========
-(void)clickBackButton:(UIButton *)button{[self dismissViewControllerAnimated:YES completion:nil];
}

3、cell中的核心代码:

-(void)setModel:(HBTextChangeModel *)model{_model = model;NSString *desc = model.content;if (desc) {//计算文本高度CGFloat height = [desc heightWithStrAttri:@{NSFontAttributeName:FONT(15), NSForegroundColorAttributeName: Color_label_DataTitle,NSParagraphStyleAttributeName:[self paragraphStyle]} withLabelWidth:SCREEN_WIDTH];if (height > 67) { //超过3行if (model.showAll) {//拼接再算高度height = [[NSString stringWithFormat:@"%@...收起",desc] heightWithStrAttri:@{NSFontAttributeName:FONT(15), NSForegroundColorAttributeName: Color_label_DataTitle,NSParagraphStyleAttributeName:[self paragraphStyle]} withLabelWidth:SCREEN_WIDTH];[self.contentBaseView mas_updateConstraints:^(MASConstraintMaker *make) {make.height.mas_equalTo(height+5);}];self.contentLabel.truncationToken = nil;[self setShowTextWithDesc:desc];} else {[self.contentBaseView mas_updateConstraints:^(MASConstraintMaker *make) {make.height.mas_equalTo(67);}];[self setAdjustableTextWithDesc:desc];}} else {[self.contentBaseView mas_updateConstraints:^(MASConstraintMaker *make) {make.height.mas_equalTo(height+10);}];NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:desc attributes:@{ NSFontAttributeName:FONT(15), NSParagraphStyleAttributeName : [self paragraphStyle]}];self.contentLabel.attributedText = text;}}
}
- (void)setShowTextWithDesc:(NSString *)desc
{NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@...收起",desc] attributes:@{ NSFontAttributeName:FONT(15), NSParagraphStyleAttributeName : [self paragraphStyle]}];WEAKSELF;//设置高亮色和点击事件[text setTextHighlightRange:[[text string] rangeOfString:@"收起"] color:[UIColor colorWithHexString:@"#0099FF"] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {weakSelf.model.showAll = NO;if (self.updateMessageCellBlock) {self.updateMessageCellBlock(@"");}}];self.contentLabel.attributedText = text;
}- (NSMutableParagraphStyle *)paragraphStyle {NSMutableParagraphStyle *para = [NSMutableParagraphStyle new];para.lineSpacing = 5.f;return para;
}- (void)setAdjustableTextWithDesc:(NSString *)desc
{NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:desc attributes:@{ NSFontAttributeName:FONT(15), NSParagraphStyleAttributeName : [self paragraphStyle]}];self.contentLabel.attributedText = text;WEAKSELF;NSMutableAttributedString *showAll = [[NSMutableAttributedString alloc] initWithString:@"...展开" attributes:nil];YYTextHighlight *hi = [YYTextHighlight new];[hi setColor:[UIColor colorWithHexString:@"#009900"]];hi.tapAction = ^(UIView *containerView,NSAttributedString *text,NSRange range, CGRect rect) {weakSelf.model.showAll = YES;if (self.updateMessageCellBlock) {self.updateMessageCellBlock(@"");}};NSRange range = [showAll.string rangeOfString:@"展开"];[showAll setColor:[UIColor colorWithHexString:@"#0099FF"] range:range];[showAll setTextHighlight:hi range:range];showAll.font = FONT(15);YYLabel *seeMore = [YYLabel new];seeMore.attributedText = showAll;[seeMore sizeToFit];seeMore.height += 2.f;NSAttributedString *truncationToken = [NSAttributedString attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:FONT(15) alignment:YYTextVerticalAlignmentTop];self.contentLabel.truncationToken = truncationToken;
}

最后附上demo连接

END.

这篇关于iOS tableviewcell里点击文字展开与收起功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/599234

相关文章

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.