UITableView的使用样例(简易向)
功能实现
- 构建一个UITableView,并使其默认显示a,b,c……..
- 构建一个按钮,点击后列表变为英文字母
- 构建一个按钮,点击后列表变为数字
基本概念
- 实现前头文件需要签订协议(如何签订向后看)UITableViewDateSource,UITableViewDelegate
- 实现UITableView必须实现的两个方法
返回:
UITableView中的section总行数
参数:
section:表示section下标
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
返回:
UITableView所有单元格(虽然在方法中是返回其中之一个单元格,但程序中它可以通过indexPath参数从0到最后循环运行下去)
参数:
indexPath:表示行的下标,可利用来读取数组的制定数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
实现过程
1.在storyboard拖拽一个UITableView,两个UIButton,然后做好相关的参数设置(签订协议不要忘了)。
实现:签订协议
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @end
实现:定义变量
@interface ViewController () @property (weak, nonatomic) IBOutlet UITableView *tvTable; @property (strong, nonatomic) NSMutableString * string; @end
2.viewDidLoad方法的实现
实现:string的先初始化为英文字母的字符串
-(void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.string = [[NSMutableString alloc]init]; for (int i = 0; i < 26; i ++) { [self.string appendFormat:@"%c",'a'+i ];//字符串末尾加上一个字符 } [self.tvTable setDataSource:self]; [self.tvTable setDelegate:self]; }
3.实现两个必须实现的方法
实现:
UITableView设定单元格个数为20个
将string字符串中的数据输入单元格
//返回单元格的数量 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; } //创建单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { static NSString *identifier = @"myCell"; UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; cell.textLabel.text = [NSString stringWithFormat:@"%c",[self.string characterAtIndex:indexPath.row]]; return cell; }
4.实现两个按键的action
- (IBAction)eventChange2Words:(UIButton *)sender { self.string = [NSMutableString stringWithFormat:@""]; for (int i = 0; i < 26; i ++) { [self.string appendFormat:@"%c",'a'+i ]; } [self.tvTable reloadData]; } - (IBAction)eventChange2Nums:(UIButton *)sender { self.string = [NSMutableString stringWithFormat:@""]; for (int i = 0; i < 26; i ++) { [self.string appendFormat:@"%c",'0'+(i%10) ]; } [self.tvTable reloadData]; }
样例结果
图中显示的是显示字母的功能,显示数字的功能其实大同小异,请自行脑补
已知缺陷
- 如果string长度小于20时程序会崩溃,因为在 - (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath中访问时访问string字符串的下标从0到19
- 你们不觉得这好丑么。。。。。
推荐阅读
OS开发系列–UITableView全面解析
http://www.cnblogs.com/kenshincui/p/3931948.html#uiTableViewCell
本文来自博客园,作者:MrYu4,转载请注明原文链接:https://www.cnblogs.com/MrYU4/p/15778902.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了