1、第一种方法:先定位到最后一行,若选中最后一行直接退出,否则用递归改变上次选中的状态,重新设置本次选中的状态。

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

{

current=indexPath.row;

}

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

if(indexPath.row==current){

return;

}

UITableViewCell*newCell =[tableViewcellForRowAtIndexPath:indexPath];

if(newCell.accessoryType==UITableViewCellAccessoryNone)

{

newCell.accessoryType=UITableViewCellAccessoryCheckmark;

newCell.textLabel.textColor=[UIColorblueColor];

}

NSIndexPath*oldIndexPath=[NSIndexPathindexPathForRow:current

];

UITableViewCell*oldCell =[tableViewcellForRowAtIndexPath:oldIndexPath];

if(oldCell.accessoryType==UITableViewCellAccessoryCheckmark)

{

oldCell.accessoryType=UITableViewCellAccessoryNone;

oldCell.textLabel.textColor=[UIColorblackColor];

}

current=indexPath.row;

}

2、第二种方法:设置一个全局变量,选中的时候传值,然后通过重新加载数据,使得在选中这行打勾,其他行无样式,此方法加载的时候第一行默认打勾了

-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

current=indexPath.row;

[self.tableView reloadData];

}

- (UITableViewCellAccessoryType)tableView:(UITableView*)tableViewaccessoryTypeForRowWithIndexPath:(NSIndexPath*)indexPath

{

if(current==indexPath.row&&current!=nil)

{

returnUITableViewCellAccessoryCheckmark;

}

else

{

returnUITableViewCellAccessoryNone;

}

}

或者直接在

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath里面设置

单元格的默认高度为44

NSLog(@"%@",NSStringFromCGRect(cell.frame));

设置选中时的背景颜色可以用selectedbackgroundview设置

3、第三种方法:在选中时先遍历整个可见单元格,设置所有行的默认样式,再设置选中的这行样式,此方法不能取消单元格的选中

-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

NSArray *array =[tableViewvisibleCells];

for (UITableViewCell *cell
in array) {

[cellsetAccessoryType:UITableViewCellAccessoryNone];

cell.textLabel.textColor=[UIColorblackColor];

}

UITableViewCell *cell=[self.tableViewcellForRowAtIndexPath:indexPath];

cell.textLabel.textColor=[UIColorblueColor];

[cell setAccessoryType:UITableViewCellAccessoryCheckmark];

}

此时只设定了在可见范围内选择的是一行,还得设置滚动后的选中状态,

-(void)tableView:(UITableView *)tableViewwillDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath

{

NSIndexPath *index=[tableView indexPathForSelectedRow];

if (index.row==indexPath.row&&index!=nil)

{

cell.backgroundColor=[UIColor colorWithRed:232.0/255.0 green:232.0/255.0blue:232.0/255.0 alpha:1.0];

cell.textLabel.textColor=[UIColor colorWithRed:0.0 green:206.0/255.0blue:192.0/255.0 alpha:1.0];

}

else

{

cell.backgroundColor=[UIColor clearColor];

cell.textLabel.textColor=[UIColor blackColor];

}

}

单元格是否相同需要用到比较方法

NSIndexPath*index=[tableViewindexPathForSelectedRow];

NSComparisonResult
result=[indexPathcompare:index];

UITableView设置单元格选中后只显示一个打勾的三种简单方法(仅供参考)的更多相关文章

  1. PHPExcel解决内存占用过大问题-设置单元格对象缓存

    PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理Excel的PHP开源类,但是很大的一个问题就是它占用内存太大,从1.7.3开始,它支持设置cell的缓存方 ...

  2. iOS UITableView 移除单元格选中时的高亮状态

    在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...

  3. [Xcode 实际操作]五、使用表格-(5)设置UITableView的单元格背景颜色

    目录:[Swift]Xcode实际操作 本文将演示单元格背景颜色的设置 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加两个协 ...

  4. Easyui datagrid 设置内容超过单元格宽度时自动换行显示

    datagrid 设置内容超过单元格宽度时自动换行显示 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 问题描述 单元格内容超过单元格宽度不会自动化换行.如下 ...

  5. Excel输入公式后只显示公式却不计算如何解决?

    在使用Excel函数公式的时候,您是否碰到过输入公式,按下Enter键之后,单元格仍只显示公式,而不显示计算结果.   工具/原料   Excel 教程以Excel2013为例 方法/步骤     教 ...

  6. iOS开发——UI_swift篇&UITableView实现单元格展开与隐藏

    UITableView实现单元格展开与隐藏  关于UITableView的展开的收缩在前面的文章我已经结束,就是使用代理,通知,block传值的时候实现的,当时是使用一个Bool值来实现,最后使用着三 ...

  7. [Xcode 实际操作]五、使用表格-(11)调整UITableView的单元格顺序

    目录:[Swift]Xcode实际操作 本文将演示如何调整单元格在表格中的位置. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先 ...

  8. C#:org.in2bits.MyXls 文本格式日期 转换,以及设置单元格格式,保留两位小数点

    org.in2bits.MyXls  Excel导入日期格式的处理 表格内容为 2014-7-22 ,导入后显示为 41842 等于一个数值,根本不是日期,后来百度了一下,发现要做如下处理: stri ...

  9. C# Aspose.Cells 如何设置单元格样式

    //Instantiating a Workbook object Workbook workbook = new Workbook(); //Adding a new worksheet to th ...

随机推荐

  1. java创建对象详解和多态问题

    一. java 构造方法不等于创建对象而是初始化对象,new 关键字分配内存和创建对象的.  二.Test test = new Test(); 有人用上面的表达式来说明构造方法返回对象引用,这是明显 ...

  2. LMAX高并发系统架构

    很早就看到过MF的这篇The LMAX Architecture,可是之前一来英文水平不够,二来确实看不懂- 今天有幸再次看到,一口气读完终于有所领悟. 1 Overall Architecture ...

  3. Team Foundation Server 2015 Update 2.1 发布日志

    微软在 2016年5月5日发布了Visual Studio Team Foundation Server 2015 update 2.1. 下面我们来看看Update2.1中给我们带来了哪些新功能. ...

  4. Hadoop:Hadoop单机伪分布式的安装和配置

    http://blog.csdn.net/pipisorry/article/details/51623195 因为lz的linux系统已经安装好了很多开发环境,可能下面的步骤有遗漏. 之前是在doc ...

  5. J2EE进阶(九)org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    org.hibernate.LazyInitializationException: could not initialize proxy - no Session 前言 在<many-to-o ...

  6. Java-IO之字符输入输出流(Reader和Writer)

    以字符为单位的输入流的公共父类是Reader: 以字符为单位的输出流的超类是Writer: 基于JDK8的Reader的源码: public abstract class Reader impleme ...

  7. Android 优质精准的用户行为统计和日志打捞方案

    Android 自定义优质精准的用户行为和日志打捞方案 Tamic csdn博客 :http://blog.csdn.net/sk719887916/article/details/51398416 ...

  8. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  9. [Err] 1093 - You can't specify target table 's' for update in FROM clause

    [Err] 1093 - You can't specify target table 's' for update in FROM clause 执行SQL DELETE from book WHE ...

  10. 【翻译】Sencha Cmd中脚本压缩方法之比较

    概述 为什么要修改默认设置 YUI压缩 Google Closure编译器 UglifyJS 案例研究Ext JS 6示例应用程序 注意事项 自定义JS压缩 小结 概述 这么多年来,Web开发人员都被 ...