这俩天学习了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 校检表的更多相关文章

  1. SQL Server ->> 校检函数CHECKSUM、CHECKSUM_AGG、BINARY_CHECKSUM和HASHBYTES

    今天特地查了一下SQL Server下的校检函数有哪些.原本我只是在工作中用过一个CHECKSUM,今天特地学习了一下才发现原来还有其他的校检函数. 这里找到了别人对于SQL SERVER下这几个校检 ...

  2. Spring MVC全局异常处理与拦截器校检

    在使用Spring MVC进行开发时,总是要对系统异常和用户的异常行为进行处理,以提供给用户友好的提示,也可以提高系统的安全性. 拦截系统响应错误 首先是拦截系统响应错误,这个可以在web.xml中配 ...

  3. 表单数据校检方法 onsubmit()的使用?

    在项目中为一个表单(from)编写onsubmit()脚本的时候,经常需要验证表单中数据的合法性 所以常会写道:<form action="/admin/addUser.do" ...

  4. UDP 校检和和算法

    #include <Winsock2.h> #include <stdio.h> #define IP_HDRINCL 2 // Header is included with ...

  5. 四则运算生成与校检 Python实现

    GitHub地址 https://github.com/little-petrol/Arithmetic.git 合作者: 郭旭 和 卢明凯 设计实现过程 代码的组织主要分为两个部分: 算法与结构体的 ...

  6. 批处理--md5校检

    @echo off rem 获取文件xx.zip的MD5 for /f "delims=" %%i in ('md5.exe xx.zip') do (set md5_var=%% ...

  7. Java 中使用正则表达式校检IP是否输入正确

    感谢大佬案例:https://www.jb51.net/article/114671.htm 正则表达式学习:(待办)近期总结

  8. Struts核心技术简介

    Struts核心技术简介 1.Struts内部机制   Struts是一种基于MVC经典设计模式的开发源代码的应用框架,它通过把Servlet.JSP.JavaBean.自定义标签和信息资源整合到一个 ...

  9. SQL SERVER 查看数据库表的字段类型,是否允许为NULL,默认值,主键等

    )-- 表名 set @table_name='bqcform101' --============表结构 select 类别,表名or字段名,描述,字段类型,是否自增,允许为NULL,默认值 fro ...

随机推荐

  1. js 等待刷新技术

  2. IOS如何延长LaunchScreen.xib启动画面

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  3. 认识ptrace函数

    认识ptrace函数 这是man对于ptrace这个系统调用的解释 http://man7.org/linux/man-pages/man2/ptrace.2.html #include <sy ...

  4. [React Testing] Reusing test boilerplate

    Setting up a shallow renderer for each test can be redundant, especially when trying to write simila ...

  5. scrollTop,scrollLeft

    document.body.scrollTop用法 网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可 ...

  6. Asp.net简单实现forms验证

    <configuration> <system.web> <compilation debug="true" targetFramework=&quo ...

  7. OD: Format String, SQL Injection, XSS

    Format String 格式化串漏洞 考虑如下的代码: #include<stdio.h> int main() { int a=44,b=77; printf("a=%d, ...

  8. Java 坦克小游戏心得

    原本是闲得慌无聊才去尝试做这个项目的,因为小时候玩小霸王的游戏机,那个时候经常玩这个游戏吧,特别是喜欢那种自定义地图的模式,觉得自由度非常不错.总之关于这个游戏,想说的一大堆.鉴于能有个空闲的时间,打 ...

  9. 解决VS2008闪退的问题

    问题:打开VS2008项目后,应该是加载完所有文件,立即断掉了IDE,查看事件器,发现图片中的错误描述,google了很久没有找到解决方案,后来还是自己动手解决这个问题花了一早上的时间,哎,只要把工程 ...

  10. IE9的window.showmodaldialog显示问题

    <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...