UITableView设置单元格选中后只显示一个打勾的三种简单方法(仅供参考)
2016-05-06 11:31
537 查看
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
inSection:0];
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&¤t!=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];
- (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
inSection:0];
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&¤t!=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];
相关文章推荐
- ios学习笔记之UIView
- Android UI性能优化实战 识别绘制中的性能问题
- UITextField左侧的表达图片(..我也不知道叫什么)
- DelayQueue的原理和使用浅谈
- Leetcode 303 Range Sum Query - Immutable
- UE4 下载4.11.2的时候:当前遇到连接问题,正在重试
- chmod命令高级篇( SetUID与SetGID标识位)
- UITextField的使用总结
- codeforces 670E. Correct Bracket Sequence Editor
- android线程间通信和主线程更新ui
- UIRectEdgeNone
- MP 新版本 [2 mysql sequelize 搭建数据库]
- PriorityBlockingQueue详解
- Request获取url各种信息的方法
- CBATTRequest 学习笔记
- 如何更好地限制一个UITextField的输入长度
- [转]UItableViewCell的复用机制
- iOS UISegmentedControl控件详解
- 安卓中关于dialog.builder问题
- poj 1141 Brackets Sequence(区间dp)