IOS开发学习笔记027-UITableView 使用模型对象
1、模型对象
2、单组数据的显示
1、模型对象
继续优化上一个程序
上一次用到字典,但是坏处多多。这里将这些数据封装到类中。
这就是MVC中得模型,模型就是数据的显示结构
新建一个类,添加几个属性和一个类方法用于快速返回对象
1 #import <Foundation/Foundation.h> 2 3 @interface Province : NSObject 4 // UI控件用weak 5 // nsstring 用copy 6 7 8 @property (nonatomic,copy) NSString *header; 9 @property (nonatomic,copy) NSString *footer; 10 @property (nonatomic,strong) NSArray *cities; 11 // 12 + (id)provinceWithHeader:(NSString *)header andFooter:(NSString *)footer andCities:(NSArray *)cities; 13 @end
1 #import "Province.h" 2 3 @implementation Province 4 5 6 + (id)provinceWithHeader:(NSString *)header andFooter:(NSString *)footer andCities:(NSArray *)cities 7 { 8 Province *pp = [[Province alloc] init]; 9 pp.footer = footer; 10 pp.header = header; 11 pp.cities = cities; 12 return pp; 13 } 14 @end
初始化对象时使用类方法
1 Province *gd = [Province provinceWithHeader:@"广东" andFooter:@"广东怒啊啊" andCities:@[@"广州",@"深圳"]]; 2 Province *hn = [Province provinceWithHeader:@"湖南" andFooter:@"湖南哈哈哈啊啊" andCities:@[@"长沙",@"岳阳"]];
修改一下 方法 numberOfRowsInSection
1 // 2 设置每组多少行 row 2 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 3 { 4 // 取出对象中得数据 5 return [_allProvince[section] cities].count; 6 }
修改一下方法 cellForRowAtIndexPath
1 // 3 返回每一行显示的内容 2 // indexPath 标识唯一的一行,第几组第几行 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 4 { 5 UITableViewCell *tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 6 // 设置cell显示的文字 7 NSString *text = [_allProvince[indexPath.section] cities][indexPath.row]; 8 tableCell.textLabel.text = text; 9 return tableCell; 10 }
修改一下方法 titleForHeaderInSection
1 #pragma mark 第section组的头部标题 2 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 3 { 4 return [_allProvince[section] header]; 5 }
在修改一下 titleForFooterInSection
1 #pragma mark 第section组的尾部显示 2 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 3 { 4 return [_allProvince[section] footer]; 5 }
效果是一样的,但是代码的可扩展性更好了。
显示表格右侧的索引
需要用法一个方法
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
返回的数组就是要现实的索引数组,单击索引文字会跳转到对应的组
1 #pragma mark 返回表格右边显示的索引条 2 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 3 { 4 NSMutableArray *title = [NSMutableArray array]; 5 for (Province *p in _allProvince) 6 { 7 [title addObject:p.header]; // 获取标题显示在索引中 8 } 9 return title; 10 }
2、单组数据的显示
以上说到的都是多组数据的显示,下面说单组数据的显示。
主要是在创建view时指定style参数为Plain
设置组和行
1 // 设置行,既然是单组,那就只有一行 2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 3 { 4 return 1; 5 } 6 // 设置行 7 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 8 { 9 return 9; 10 } 11 // 设置行内容 12 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 13 { 14 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 15 cell.textLabel.text = [NSString stringWithFormat:@"第%d行数据",indexPath.row]; // 中间文字 16 cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"00%d.png",indexPath.row + 1]]; // 左侧图像 17 cell.detailTextLabel.text = [NSString stringWithFormat:@"第%d行数据的描述",indexPath.row]; //描述文字,对textLable的描述 18 return cell; 19 }
其中UITableViewCell的几种显示方式:
UITableViewCellStyleDefault:不显示子标题
UITableViewCellStyleValue1:detial标签显示在右边
UITableViewCellStyleValue2:不显示图片
UITableViewCellStyleSubTitle:显示子标题
设置显示在最右侧的按钮或者图标
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 最右侧指示信息
UITableViewCellAccessoryCheckmark 最右侧显示一个对号
UITableViewCellAccessoryDetailButton 最右侧显示一个i按钮
UITableViewCellAccessoryDetailDisclosureButton 显示一个I按钮和一个尖括号>
UITableViewCellAccessoryDisclosureIndicator 显示一个尖括号 >
最终效果是这样
代码如下
1 // 2 // SLQViewController.m 3 // UITableView-单组数据显示 4 // 5 // Created by Christian on 15/5/16. 6 // Copyright (c) 2015年 slq. All rights reserved. 7 // 8 9 #import "SLQViewController.h" 10 11 @interface SLQViewController () <UITableViewDataSource, UITableViewDelegate> 12 13 @end 14 15 @implementation SLQViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 // Do any additional setup after loading the view, typically from a nib. 21 22 } 23 24 25 // 设置行,既然是单组,那就只有一行 26 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 27 { 28 return 1; 29 } 30 // 设置行 31 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 32 { 33 return 9; 34 } 35 // 设置行内容 36 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 37 { 38 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 39 cell.textLabel.text = [NSString stringWithFormat:@"第%ld行数据",indexPath.row]; // 中间文字 40 cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"00%ld.png",indexPath.row + 1]]; // 左侧图像 41 cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld行数据的描述",indexPath.row]; // 描述信息 42 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 最右侧指示信息 43 return cell; 44 } 45 46 47 @end
3、选中行后弹出对话框
弹出对话框使用UIAlertView,遵守代理UIAlertViewDelegate的对象都可弹出UIAlertView对话框
获取选中行的方法是 didSelectRowAtIndexPath
1 // 选中了某一行的cell就会调用2 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath3 {4 // 1、取出点击的行对象5 Shop *shop = _shops[indexPath.row] ;6 // 2、创建UIAlertView提示窗口,指定代理7 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:shop.name delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];8 // 3、设置窗口显示样式,以明文显示还是以密文显示9 alert.alertViewStyle = UIAlertViewStylePlainTextInput; 10 // 4、设置输入文本框默认显示的文字 11 [alert textFieldAtIndex:0].text = shop.name; 12 // 5、显示 13 [alert show]; 14 // 6、绑定显示的行号,在代理方法中更新数据 15 alert.tag = indexPath.row; 16 }
然后弹出对话框后对数据进行修改,最后将修改后的数据更新到表格中,单击确定按钮保存结果
1 // alertview的代理方法,在创建alertView时传递代理2 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex3 {4 // 1、取消按钮直接返回5 if(buttonIndex == 0) return ;6 // 2、点击的时确定按钮7 // 2.1获取字符串8 NSString *text = [alertView textFieldAtIndex:0].text;9 // 2.2修改模型数据 10 int row = alertView.tag; 11 Shop *shop = _shops[row]; 12 shop.name = text; 13 // 2.3更新行,tableView给UITableView绑定的变量 14 [_tableView reloadData]; // 重新刷新所有行 15 16 }
reloadData方法刷新所有行,如果只修改一行,显然没必要刷新所有行。刷新某一行使用方法 reloadRowsAtIndexPaths
1 // 刷新某一行,自带动画效果 2 NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; 3 [_tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
转载于:https://www.cnblogs.com/songliquan/p/4507556.html
IOS开发学习笔记027-UITableView 使用模型对象相关推荐
- ios开发学习笔记--Core Motion
iOS开发学习笔记之CoreMotion-运动传感器 官网文档:CoreMotion Framework Reference 一. 简介 现在的苹果手机都基本有运动传感器,能够过获取到设备的加 ...
- IOS开发学习笔记-----UILabel 详解
IOS开发学习笔记-----UILabel 详解 01 //创建uilabel 02 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMa ...
- IOS开发学习笔记(一)
概述: iOS是苹果开发的手持设备操作系统(iPhone,iPad,iPod touch,iPad mini). 基于UNIX,层次架构:核心操作系统层(Core OS)-> 核心服务层(Cor ...
- Polyworks脚本开发学习笔记(十九)-将数据对象与参考对象对齐的方法
Polyworks脚本开发学习笔记(十九)-将数据对象与参考对象对齐的方法 把开发手册理了一遍,发现还有几个点没有记录下来,其中一个就是使用点对的粗对齐和使用参考目标的精确对齐.为了把这个学习笔记凑够 ...
- iOS开发学习笔记-C语言学习(一)
1.什么是iOS? 提问:买来一台新电脑后,应该先干什么? iOS就是搭载在iPhone .iPad.iPod.Apple TV上的操作系统. 苹果系统 那么,操作系统是什么呢? 操作系统简单来说就是 ...
- ios开发学习笔记--调用相册或相机(UIImagePickerController)
Ios开发相册和相机的使用-UIImagePickerController 在开发中,有时候需要获取用户的相册或者调用相机采集图片,比如APP的头像,此时可以使用UIImagePickerContro ...
- IOS开发学习笔记(1)
最近参与到IOS项目的开发中,原来只接触过一点手机开发,但是对IOS开发是毫无经验.原来一直很排斥Objective-C,排斥的原因主要有这么几点.首先Objective-C是C的扩展,我对C就不是很 ...
- iOS开发学习笔记二:UITableView(1)
一:TableViewController 1:删掉默认的ViewController 拖动一个TableViewController 2:新建一个Cocoa Touch Class,命名为:Tabl ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
参考:http://blog.csdn.net/mad1989/article/details/7972612 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置backgr ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用) 其实在代码里还是可以设置的,那就是删除背景view [[ ...
最新文章
- Android Fragment 调用宿主Activity 里面的方法
- python 常用PEP8规范
- ExtJs学习笔记(2)_Basic GridPanel[基本网格]
- 为什么手机网速太慢_手机明明是满格信号,为什么网速却非常慢?原来是这些功能在捣鬼...
- Xcode常用技巧(1)-使用Xcode进行代码分析及GDB调试
- 2019液晶电视机质量排名前十名
- shell脚本大全(一)
- 最基础的傅里叶变换公式推导
- 【解决】nacos Ignore the empty nacos configuration and get it based on dataId
- 用java做考试管理系统,考试管理系统的开发实现(Java+Web)
- Android开发——kotlin语法基础
- SDN相关组织之ODL(opendaylight)
- 2022年软件工程师报告出炉,年薪最高的是...
- 服务器里的文件怎么删除
- 基于SSM的医院挂号就诊系统
- 关于野火开发板stm32f103VET(拂晓开发板)DAPCMSIS-DAP下载烧录程序失败的问题及解决
- 利用RSA非对称加密对文本信息进行加密
- AUTOCAD——面域
- 如何用VScode 进行调试
- 拜托,你的这些努力一定要让HR看见!