ios tableView选中一个cell 的checkbox 每隔8个cell的checkbox 都会被选中
来源:互联网 发布:c语言void的用法 编辑:程序博客网 时间:2024/06/10 14:44
1、问题
奇怪的BUG:
用的是UITableView 每个cell 里面加了一个 checkbox框 , 通过两个图片来切换 选中、未选中
点击事件如下,但是发现:如果选中第1个cell, 那么第8个 第16个。。。。(通过滚动。。出来的cell)都被选中了, 这个Bug原来是缓存造成的。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
UITableViewCell *cell = (UITableViewCell*)[tableViewcellForRowAtIndexPath:indexPath];
UIImageView *checkedImage = (UIImageView *)[cell.contentViewviewWithTag:checkedTag];
NSUInteger row = [indexPathrow];
//Airconditioner ac 是UI的业务数据model,也就是当ac 的checked 是YES的时候,UI为选中图片,反之。。。
Airconditioner *ac = [acArrayobjectAtIndex:row];
if (ac.checked ==nil) {
ac.checked=@"NO";
}
if ([ac.checkedisEqualToString:@"NO"]) {
ac.checked =@"YES";
checkedImage.image = [UIImageimageNamed:@"checked.png"];
cell.backgroundColor = [UIColorcolorWithRed:223.0/255.0green:230.0/255.0blue:250.0/255.0alpha:1.0];
}else {
ac.checked =@"NO";
checkedImage.image = [UIImageimageNamed:@"unchecked.png"];
cell.backgroundColor = [UIColorwhiteColor];
}
}
2、解决办法
-(UITableViewCell *)customCellWithOutXib:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath{
//定义标识符
staticNSString *customCellIndentifier =@"CustomCellIndentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:customCellIndentifier];
//定义新的cell
if(cell ==nil){
//使用默认的UITableViewCell,但是不使用默认的image与text,改为添加自定义的控件
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:customCellIndentifier];
。。。。。。。。。。。其它UI 元素。。。。。。。。。。。。
//checked图片
CGRect checkedRect =CGRectMake(320,15,30,30);
UIImageView *checkedImageView = [[UIImageViewalloc]initWithFrame:checkedRect];
checkedImageView.tag =checkedTag;
checkedImageView.image = [UIImageimageNamed:@"unchecked.png"];
//为图片添加边框
CALayer *checkedLayer = [checkedImageViewlayer];
checkedLayer.cornerRadius =7;
checkedLayer.borderColor = [[UIColorwhiteColor]CGColor];
checkedLayer.borderWidth =1;
checkedLayer.masksToBounds =YES;
[cell.contentViewaddSubview:checkedImageView];
}
//获得行数
NSUInteger row = [indexPathrow];
UIImageView *checkedImage = (UIImageView *)[cell.contentViewviewWithTag:checkedTag];
// ac 是UI的数据model,也就是当ac 的checked 是YES的时候,UI为选中图片,反之。。。
if (ac.checked ==nil) {
ac.checked=@"NO";
}
if ([ac.checkedisEqualToString:@"NO"]) {
checkedImage.image = [UIImageimageNamed:@"unchecked.png"];
cell.backgroundColor = [UIColorwhiteColor];
}else {
checkedImage.image = [UIImageimageNamed:@"checked.png"];
cell.backgroundColor = [UIColorcolorWithRed:223.0/255.0green:230.0/255.0blue:250.0/255.0alpha:1.0];
}
return cell;
}
- ios tableView选中一个cell 的checkbox 每隔8个cell的checkbox 都会被选中
- iOS tableview获取当前选中的cell
- tableView 的Cell的选中状态
- 获得tableview当前选中的cell
- 获取tableview中当前选中的cell
- 获取tableView当前选中的cell
- tableView cell 选中颜色的改变
- TableView 点击cell,改变选中cell的高度
- TableView 点击cell,改变选中cell的高度
- iphone开发 tableview的cell选中、非选中背景图
- 默认tableview选中第一个cell
- iOS tableView刷新选中cell数据
- iOS Uitableview的cell的选中样式
- iOS Uitableview的cell的选中样式
- 当cell 被选中的时候,cell内部的所有的子控件都会达到高亮状态
- iOS中 cell的选中颜色以及默认选中行
- radio,checkbox的选中
- checkbox选中的问题
- 王者归来
- 数据库优化查询性能
- Unix试卷
- 使用SoftEthe利用vps服务器搭建vpn(ipv6可用)
- CentOS 7安装mysql
- ios tableView选中一个cell 的checkbox 每隔8个cell的checkbox 都会被选中
- 读书笔记(二)
- 往QStandarItemModel插入数据的正确姿势
- 一.Android单元测试 Mockito的简单用法
- 关于开发中的AppbarLayout问题
- Android播放Gif动态图片的几种方式
- 数字视音频处理知识点小结
- Code blocks
- Ruby中的类与对象(一)