TableViewCellEdit

本文介绍了一个iOS应用中的TableView懒加载实现方式,通过延迟加载数据减少内存消耗,并展示了如何根据不同条件使用自定义Cell及调整Cell高度以适应内容。

//

//  RootViewController.m

 

#import "RootViewController.h"

#import "Student.h"

#import "MyTableViewCell.h"

#import "SecTableViewCell.h"

#import "AjustFrame.h"

 

@interfaceRootViewController()<UITableViewDataSource,UITableViewDelegate>

 

@property (nonatomic,strong)UITableView*tableView;

@property (nonatomic,strong)NSMutableArray*dataArr;

 

@end

 

@implementationRootViewController

 

- (void)viewDidLoad {

   [superviewDidLoad];

 

self.tableView =[[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStyleGrouped];

 

self.tableView.dataSource = self;

self.tableView.delegate = self;

   [self.viewaddSubview:self.tableView];

 

}

    // 懒加载即重写了dataArr属性的getter方法。

   //懒加载,只有调用了getter方法的时候才会加载数据,能够减少viewDidLoad的负担,减少内存消耗

- (NSMutableArray *)dataArr{

if (_dataArr == nil) {

       // 创建数组,用来存储学生对象

self.dataArr = [NSMutableArray array];

       // 获取Students.plist文件

NSString *file =[[NSBundlemainBundle]pathForResource:@"Students"ofType:@"plist"];

       // 获取文件中的数据

NSMutableArray *arr =[NSMutableArrayarrayWithContentsOfFile:file];

       // 将数据和model类联系起来

       // 数据解析

for (NSDictionary *dic in arr) {

           // 定义一个Student对象

           Student *stu = [[Student alloc]init];

           // 将学生与字典对应

           [stusetValuesForKeysWithDictionary:dic];

           // 将学生存进数组

           [self.dataArraddObject:stu];

       }

    }

return _dataArr;

}

 

     //  获取数据,通过 [selfgetData] 使用

//- (void)getData{

//    // 创建数组

//   self.dataArr = [NSMutableArray array];

//   // 获取Students.plist文件

//   NSString *file = [[NSBundlemainBundle]pathForResource:@"Students"ofType:@"plist"];

//   // 获取文件中的数据

//   NSMutableArray *arr = [NSMutableArrayarrayWithContentsOfFile:file];

//   // 将数据和model类联系起来

//   // 数据解析

//   for (NSDictionary *dic in arr) {

//       // 定义一个Student对象

//       Student *stu = [[Student alloc]init];

//       // 将学生与字典对应

//       [stusetValuesForKeysWithDictionary:dic];

//       // 将学生存进数组

//       [self.dataArraddObject:stu];

//   }

//   

//}

 

 

- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{

return 7;

}

       // 从重用列表中获取cell或者重新创建一个cell

- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{

   // 首先得到学生

   Student *stu = self.dataArr[indexPath.row];

   // 判断学生性别决定使用哪种自定义cell

   if ([stu.sexisEqualToString:@"男"]) {

MyTableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:@"reuse"];

if (cell == nil) {

cell =[[MyTableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:@"reuse"];

       }

       Student *student = self.dataArr[indexPath.row];

cell.stu = student;

return cell;

}else{

SecTableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:@"log"];

if (cell == nil) {

cell =[[SecTableViewCellalloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"log"];

       }

       Student *student = self.dataArr[indexPath.row];

cell.student = student;

return cell;

    }

}

 

     // 计算cell的高度

- (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{

//    // 得出对应单元格的学生

//   Student *stu = self.dataArr[indexPath.row];

//   // 得出字符串的文本属性

//   NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};

//    // 计算字符串所占高度(label的高度)

//   CGFloat height = [stu.introduceboundingRectWithSize:CGSizeMake(200,1000)options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size.height;

//   // cell的高度就是字符串的高度加上label的高度

//   return height + 130;

 

        // 自适应高度,根据cell中内容的多少设置高度

      // 获取学生对象

    Student *stu = self.dataArr[indexPath.row];

      // 计算学生对象中字符串stu.introduce的内容高度

CGFloat height =[AjustFrameheightForString:stu.introduce width:200 Font:[UIFontsystemFontOfSize:17]];

return height + 110;

}

@end

 

 

 

 

 

//

//  SecTableViewCell.m


#import "SecTableViewCell.h"

#import "AjustFrame.h"

 

@implementationSecTableViewCell

 

- (void)awakeFromNib {

   // Initialization code

}

 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

   [supersetSelected:selectedanimated:animated];

 

 

}

 

    // 自定义的cell

-(instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier{

self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];

if (self) {

self.imageV =[[UIImageViewalloc]initWithFrame:CGRectMake(250, 5, 150, 180)];

       [self.contentViewaddSubview:self.imageV];

 

self.nameLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 5, 200, 30)];

       [self.contentViewaddSubview:self.nameLabel];

 

self.phoneNumberLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 40, 200, 30)];

       [self.contentViewaddSubview:self.phoneNumberLabel];

 

self.sexLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 75, 200, 30)];

       [self.contentViewaddSubview:self.sexLabel];

 

self.introduceLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 110, 200, 30)];

self.introduceLabel.numberOfLines = 0;

       [self.contentViewaddSubview:self.introduceLabel];

 

    }

return self;

}

 

- (void)setStudent:(Student *)student{

self.imageV.image =[UIImageimageNamed:student.icon];

self.nameLabel.text = student.name;

self.phoneNumberLabel.text =student.phoneNumber;

self.sexLabel.text = student.sex;

self.introduceLabel.text =student.introduce;

 

      // 获取introduceLabel中字符串的高度

CGFloat height =[AjustFrameheightForString:self.introduceLabel.text width:200Font:self.introduceLabel.font];

   // 重新设置introducelabel的大小

self.introduceLabel.frame = CGRectMake(5,110, 200, height);

 

}

 

@end

 

 

 

 

//

//  MyTableViewCell.m


#import "MyTableViewCell.h"

#import "AjustFrame.h"

 

@implementationMyTableViewCell

 

- (void)awakeFromNib {

 

}

 

- (void)setSelected:(BOOL)selectedanimated:(BOOL)animated {

   [supersetSelected:selectedanimated:animated];

 

 

}

 

//自定义视图,重新实现初始化方法

-(instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier{

self = [super initWithStyle:stylereuseIdentifier:reuseIdentifier];

if (self) {

self.imageV =[[UIImageViewalloc]initWithFrame:CGRectMake(5, 5, 120, 150)];

       [self.contentViewaddSubview:self.imageV];

 

 

self.nameL =[[UILabelalloc]initWithFrame:CGRectMake(130, 5, 200, 30)];

       [self.contentViewaddSubview:self.nameL];

 

 

self.sexL =[[UILabelalloc]initWithFrame:CGRectMake(130, 40, 200, 30)];

       [self.contentViewaddSubview:self.sexL];

 

 

self.phoneNumberL =[[UILabelalloc]initWithFrame:CGRectMake(130, 75, 200, 30)];

        [self.contentViewaddSubview:self.phoneNumberL];

 

 

self.introduceL =[[UILabelalloc]initWithFrame:CGRectMake(130, 110, 250, 60)];

self.introduceL.numberOfLines = 0;

       [self.contentViewaddSubview:self.introduceL];

 

    }

return self;

}

 

   //为控件内容赋值

- (void)setStu:(Student *)stu{

self.imageV.image =[UIImageimageNamed:stu.icon];

self.nameL.text = stu.name;

self.phoneNumberL.text = stu.phoneNumber;

self.sexL.text = stu.sex;

self.introduceL.text = stu.introduce;

//   // 控件的大小应该在拿到控件的数据之后确定

//   // 求出文本高度

//    // 求字符串的字体大小

//   NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};

//   // 求出所占高度

//   CGFloat height =[self.introduceL.textboundingRectWithSize:CGSizeMake(200, 1000)options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size.height;

//   // 重新设置label高度

//   self.introduceL.frame = CGRectMake(130, 110, 200, height);

 

     // 优化的代码

CGFloat height =[AjustFrameheightForString:self.introduceL.text width:200Font:self.introduceL.font];

self.introduceL.frame = CGRectMake(130,110, 250, height);

}

 

@end

 

 

 

 

 

 

//声明一个类,专门用来计算字符串的高度,从而使cell能够根据内容的多少来计算高度。

// AjustFrame.m

 

#import "AjustFrame.h"

 

@implementationAjustFrame

 

    // 用来计算字符串的高度

+ (CGFloat)heightForString:(NSString *)textwidth:(CGFloat)width Font:(UIFont *)font{

CGFloat height = [textboundingRectWithSize:CGSizeMake(width, 1000)options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font}context:nil].size.height;

 

return height;

}

 

@end

 

本项目通过STM32F103C8T6单片机最小系统,连接正点原子ESP8266 WiFi模块,将模块设置为Station模式,并与电脑连接到同一个WiFi网络。随后,STM32F103C8T6单片机将数据发送到电脑所在的IP地址。 功能概述 硬件连接: STM32F103C8T6单片机与正点原子ESP8266 WiFi模块通过串口连接。 ESP8266模块通过WiFi连接到电脑所在的WiFi网络。 软件配置: 在STM32F103C8T6上配置串口通信,用于与ESP8266模块进行数据交互。 通过AT指令将ESP8266模块设置为Station模式,并连接到指定的WiFi网络。 配置STM32F103C8T6单片机,使其能够通过ESP8266模块向电脑发送数据。 数据发送: STM32F103C8T6单片机通过串口向ESP8266模块发送数据。 ESP8266模块将接收到的数据通过WiFi发送到电脑所在的IP地址。 使用说明 硬件准备: 准备STM32F103C8T6单片机最小系统板。 准备正点原子ESP8266 WiFi模块。 将STM32F103C8T6单片机与ESP8266模块通过串口连接。 软件准备: 下载并安装STM32开发环境(如Keil、STM32CubeIDE等)。 下载本项目提供的源代码,并导入到开发环境中。 配置与编译: 根据实际需求配置WiFi网络名称和密码。 配置电脑的IP地址,确保与ESP8266模块在同一网络中。 编译并下载程序到STM32F103C8T6单片机。 运行与测试: 将STM32F103C8T6单片机与ESP8266模块上电。 在电脑上打开网络调试工具(如Wireshark、网络调试助手等),监听指定端口。 观察电脑是否接收到来自STM32F103C8T6单片机发送的数据。
在电子测量技术中,示波装置扮演着观测电信号形态的关键角色。然而,市售标准示波器往往定价较高,使得资源有限的入门者或教学环境难以配备。为此,可采用基于51系列微控制器的简易示波方案进行替代。该方案虽在性能上不及专业设备,但已能满足基础教学与常规电路检测的需求。下文将系统阐述该装置的主要构成模块及其运行机制。 本装置以51系列单片机作为中央处理核心,承担信号数据的运算与管理任务。该单片机属于8位微控制器家族,在嵌入式应用领域使用广泛。其控制程序可采用C语言进行开发,得益于C语言在嵌入式编程中的高效性与适应性,它成为实现该功能的合适选择。 波形显示部分采用了由ST7565控制器驱动的128×64点阵液晶模块。ST7565是一款图形液晶驱动芯片,支持多种像素规格的显示输出;此处所指的12864即表示屏幕具有128列、64行的像素阵列。该屏幕能以图形方式实时绘制信号曲线,从而提供直观的观测界面。 在模拟至数字信号转换环节,系统集成了TLC0820型模数转换芯片。该芯片具备8位分辨率及双输入通道,最高采样速率可达每秒10万次。这样的转换速度对于捕获快速变动的信号波形具有重要意义。 实现该示波装置需综合运用嵌入式软硬件技术。开发者需掌握51单片机的指令系统与编程方法,熟悉ST7565控制器的显示驱动配置,并能对TLC0820芯片进行正确的采样编程。此外,还需设计相应的模拟前端电路,包括信号调理、放大与滤波等部分,以确保输入ADC的信号质量满足测量要求。 通过C语言编写的控制程序,可完成系统各模块的初始化、数据采集、数值处理以及图形化显示等完整流程。开发过程中需借助调试工具对代码进行验证,保证程序执行的正确性与稳定性。 应当指出,受限于51系列单片机的运算能力与资源,该自制装置的功能相对基础,例如难以实现多通道同步测量、高级触发模式或高容量波形存储等复杂特性。尽管如此,对于绝大多数基础电子实验与教学演示而言,其性能已足够适用。 综上所述,结合51单片机、ST7565液晶控制器与TLC0820转换芯片,可以构建出一套成本低廉、结构清晰的简易示波系统。该装置不仅可作为电子爱好者、在校学生及教师的有益实践平台,帮助理解示波测量的基本原理,还能通过动手组装与调试过程,深化对电路分析与嵌入式系统设计的认识。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值