TableView 校检表
这俩天学习了tableView 校检表 主要就是通过一个方法来跟踪当前选中的行。下面将声明一个NSIndexPath 的属性来跟踪最后选中的行。这篇文章希望能给那些初学者带来学习的乐趣。不说了直接上代码。
首先在AppDelegate.m中声明一个控制器 navController基于TableView
BIDFirstLevelController *first = [[BIDFirstLevelController alloc]
initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc]
initWithRootViewController:first];
[self.window addSubview:navController.view];
然后创建新文件基于UITableViewController 的 TableView 的文件。在.h声明两个属性list 和*lastIndexPath;Path
@property (strong, nonatomic) NSArray *list;
@property (strong, nonatomic) NSIndexPath *lastIndexPath;
别忘了添加TbaleView 的协议。然后就是在.m里面去实现各种方法
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"Who Hash",
@"Bubba Gump Shrimp Étouffée", @"Who Pudding", @"Scooby Snacks",
@"Everlasting Gobstopper", @"Green Eggs and Ham", @"Soylent Green",
@"Hard Tack", @"Lembas Bread", @"Roast Beast", @"Blancmange", nil];
self.list = array;
}
- (void)viewDidUnload {
[super viewDidUnload];
self.list = nil;
self.lastIndexPath = nil;
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CheckMarkCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CheckMarkCellIdentifier];
}
NSUInteger row = [indexPath row];//从这个单元和当前选项中提取行;
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [list objectAtIndex:row];//从数组中获得这一行的值,并将它分配给单元的标题
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
然后,根据两行是否相同,将扩展图标设置为显示检验标记或者不显示任何东西。换句话说如果某行的表正在请求单元,而这行正好是当前选中的行,我们就将就扩展图标设置为一个选中标记;否则他就不显示任何东西。注意,还需要检查lastIndexPath来确定它不为nil 。这样做是因为值为nil 的lastIndex 表示没有任何选项。但是在nil对象上调用row的方法将返回0 他是一个有效行,不过我们不希望在0行上放置一个检验标记。因为实际上,没有任何选项。
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
//新旧两行如果相同就不做更改
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
self.lastIndexPath = indexPath;//存储刚才在lastIndex中选中的索引路线,以便再下一次选中一行时使用:
}
//完成之后,告知表示图取消选中刚才的行,因为我们不希望该行一直保持突出显示。我们已经选中标记了改行,把他保留为蓝色将是很麻烦的一件事。
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
下一步,获取刚才选中的单元,并制定一个校检标记作为它的扩展图标,然后获取上一次选中的单元,将他的扩展图标设置为无
TableView 校检表的更多相关文章
- SQL Server ->> 校检函数CHECKSUM、CHECKSUM_AGG、BINARY_CHECKSUM和HASHBYTES
今天特地查了一下SQL Server下的校检函数有哪些.原本我只是在工作中用过一个CHECKSUM,今天特地学习了一下才发现原来还有其他的校检函数. 这里找到了别人对于SQL SERVER下这几个校检 ...
- Spring MVC全局异常处理与拦截器校检
在使用Spring MVC进行开发时,总是要对系统异常和用户的异常行为进行处理,以提供给用户友好的提示,也可以提高系统的安全性. 拦截系统响应错误 首先是拦截系统响应错误,这个可以在web.xml中配 ...
- 表单数据校检方法 onsubmit()的使用?
在项目中为一个表单(from)编写onsubmit()脚本的时候,经常需要验证表单中数据的合法性 所以常会写道:<form action="/admin/addUser.do" ...
- UDP 校检和和算法
#include <Winsock2.h> #include <stdio.h> #define IP_HDRINCL 2 // Header is included with ...
- 四则运算生成与校检 Python实现
GitHub地址 https://github.com/little-petrol/Arithmetic.git 合作者: 郭旭 和 卢明凯 设计实现过程 代码的组织主要分为两个部分: 算法与结构体的 ...
- 批处理--md5校检
@echo off rem 获取文件xx.zip的MD5 for /f "delims=" %%i in ('md5.exe xx.zip') do (set md5_var=%% ...
- Java 中使用正则表达式校检IP是否输入正确
感谢大佬案例:https://www.jb51.net/article/114671.htm 正则表达式学习:(待办)近期总结
- Struts核心技术简介
Struts核心技术简介 1.Struts内部机制 Struts是一种基于MVC经典设计模式的开发源代码的应用框架,它通过把Servlet.JSP.JavaBean.自定义标签和信息资源整合到一个 ...
- SQL SERVER 查看数据库表的字段类型,是否允许为NULL,默认值,主键等
)-- 表名 set @table_name='bqcform101' --============表结构 select 类别,表名or字段名,描述,字段类型,是否自增,允许为NULL,默认值 fro ...
随机推荐
- Jquery css函数用法(判断标签是否拥有某属性)
判断一个层是否隐藏:$("#id").css("display")=="none" ;在所有匹配的元素中,设置一个样式属性的值:$(&qu ...
- 【实验 1-1】编写一个简单的 TCP 服务器和 TCP 客户端程序。程序均为控制台程序窗口。
在新建的 C++源文件中编写如下代码. 1.TCP 服务器端#include<winsock2.h> //包含头文件#include<stdio.h>#include<w ...
- HDU 2435 There is a war (网络流-最小割)
There is a war Problem Description There is a sea. There are N islands in the sea. ...
- JAVA学习篇--javaweb之Filter具体解释
在DRP项目中,多次提到了Filter,它攻克了字符集的统一设置以及统一控制简单WebCache,从中我们能够体会到.它给我们带来的优点不不过降低代码量这么简单,它的出现避免了我们每一个页面反复的编写 ...
- [Linux 小技巧] Ubuntu 14.04 下编译、安装、配置最新开发版 GoldenDict
1. 背景介绍 GoldenDict 是一款非常优秀的跨平台电子词典软件,支持 StarDict.Babylon 等多种词典.其 PC 版基于 Qt 编写,完全免费.开源且没有广告.GoldenDic ...
- SOAP 简单对象访问协议
webService三要素 SOAP.WSDL(WebServicesDescriptionLanguage).UDDI(UniversalDescriptionDiscovery andIntegr ...
- VMware Virtual Machine安装报错解决1
安装完VMware virtual machine 后,再进行 "create a new virtual machine"最后点击"Finish"时,报如下错 ...
- hdu 2013
水题 AC代码: #include <iostream> using namespace std; int main() { int i,m,n; while(cin>>n) ...
- Asp.Net WebAPI 通过HttpContextBase获取请求参数
WEBAPI中的Request是HttpRequestMessage类型,不能像Web传统那样有querystring和from 方法接收参数,而传统的HttpReqest的基类是HttpReqest ...
- java 连接sql server2008配置
Java 应用程序连接SQL Server2008 (Eclipse+JDK7.0+jdbc4.0.jar+Sql Server2008) 假设应用端的连接语句为: String url = &quo ...