iOS开发-UITableView-5(cell对勾)
2015-07-10 16:23
483 查看
今天做个表格,突然发现在选中某行时打勾,再次选中其它行时,上次选中的行的勾还在,不能自动消失。
于是试了以下3种方法:
1、// 第一种方法:在选中时先遍历整个可见单元格,设置所有行的默认样式,再设置选中的这行样式,此方法不能取消单元格的选中
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSArray *array = [tableView visibleCells];
for (UITableViewCell *cell in array)
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.textLabel.textColor=[UIColor blackColor];
}
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor=[UIColor blueColor];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
此时只设定了在可见范围内选择的是一行,还得设置滚动后的选中状态,
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(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=[tableView indexPathForSelectedRow];
NSComparisonResult result=[indexPath compare:index];
2、//第二种方法:先定位到最后一行,若选中最后一行直接退出,否则用递归改变上次选中的状态,重新设置本次选中的状态。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
current=indexPath.row;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.row==current){
return;
}
UITableViewCell*newCell = [tableView cellForRowAtIndexPath:indexPath];
if(newCell.accessoryType==UITableViewCellAccessoryNone)
{
newCell.accessoryType=UITableViewCellAccessoryCheckmark;
newCell.textLabel.textColor=[UIColor blueColor];
}
NSIndexPath*oldIndexPath =[NSIndexPath indexPathForRow:current
inSection:0];
UITableViewCell*oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if(oldCell.accessoryType==UITableViewCellAccessoryCheckmark)
{
oldCell.accessoryType=UITableViewCellAccessoryNone;
oldCell.textLabel.textColor=[UIColor blackColor];
}
current=indexPath.row;
}
3.//方法三:设置一个全局变量,选中的时候传值,然后通过重新加载数据,使得在选中这行打勾,其他行无样式,此方法加载的时候第一行默认打勾了
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
current=indexPath.row;
[self.tableView reloadData];
}
- (UITableViewCellAccessoryType)tableView:(UITableView*)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath*)indexPath
{
if(current==indexPath.row&¤t!=nil)
{
return UITableViewCellAccessoryCheckmark;
}
else
{
return UITableViewCellAccessoryNone;
}
}
或者直接在
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath里面设置
单元格的默认高度为44
NSLog(@"%@",NSStringFromCGRect(cell.frame));
设置选中时的背景颜色可以用selectedbackgroundview设置
于是试了以下3种方法:
1、// 第一种方法:在选中时先遍历整个可见单元格,设置所有行的默认样式,再设置选中的这行样式,此方法不能取消单元格的选中
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSArray *array = [tableView visibleCells];
for (UITableViewCell *cell in array)
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.textLabel.textColor=[UIColor blackColor];
}
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor=[UIColor blueColor];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
此时只设定了在可见范围内选择的是一行,还得设置滚动后的选中状态,
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(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=[tableView indexPathForSelectedRow];
NSComparisonResult result=[indexPath compare:index];
2、//第二种方法:先定位到最后一行,若选中最后一行直接退出,否则用递归改变上次选中的状态,重新设置本次选中的状态。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
current=indexPath.row;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.row==current){
return;
}
UITableViewCell*newCell = [tableView cellForRowAtIndexPath:indexPath];
if(newCell.accessoryType==UITableViewCellAccessoryNone)
{
newCell.accessoryType=UITableViewCellAccessoryCheckmark;
newCell.textLabel.textColor=[UIColor blueColor];
}
NSIndexPath*oldIndexPath =[NSIndexPath indexPathForRow:current
inSection:0];
UITableViewCell*oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if(oldCell.accessoryType==UITableViewCellAccessoryCheckmark)
{
oldCell.accessoryType=UITableViewCellAccessoryNone;
oldCell.textLabel.textColor=[UIColor blackColor];
}
current=indexPath.row;
}
3.//方法三:设置一个全局变量,选中的时候传值,然后通过重新加载数据,使得在选中这行打勾,其他行无样式,此方法加载的时候第一行默认打勾了
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
current=indexPath.row;
[self.tableView reloadData];
}
- (UITableViewCellAccessoryType)tableView:(UITableView*)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath*)indexPath
{
if(current==indexPath.row&¤t!=nil)
{
return UITableViewCellAccessoryCheckmark;
}
else
{
return UITableViewCellAccessoryNone;
}
}
或者直接在
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath里面设置
单元格的默认高度为44
NSLog(@"%@",NSStringFromCGRect(cell.frame));
设置选中时的背景颜色可以用selectedbackgroundview设置
相关文章推荐
- iOS开发-UITableView-4(多选)
- 划线 UIGraphicsBeginImageContext
- iOS开发-UITableView-3(左右滑动)
- Java 7中的TransferQueue
- spark web ui中的skipped的含义
- 从ETTI Touch 到 uevent 机制分析
- iOS开发-UITableView-2
- Coded UI Test 同时执行多个测试用例不必每次都关闭浏览器
- SOUI 界面之tabctrl控件
- String、StringBuffer和StringBuilder
- iOS-开发UITableView-1
- 使用UIImagePickerController拍照和视频录制
- 2678 - Subsequence
- 获取xcode version和build
- (边学边记录)UIGestureRecognizer
- (转) 通过UUID在vSphere虚拟机内外识别硬盘
- java Long的iniValue出错
- codeforces 450B Jzzhu and Sequences
- CSS3动画那么强,requestAnimationFrame还有毛线用。
- UITableViewCell复用机制