惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
T
Threatpost
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
S
Securelist
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
S
Secure Thoughts
MyScale Blog
MyScale Blog
O
OpenAI News
P
Palo Alto Networks Blog
美团技术团队
C
Cyber Attacks, Cyber Crime and Cyber Security
TaoSecurity Blog
TaoSecurity Blog
量子位
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
Know Your Adversary
Know Your Adversary
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Simon Willison's Weblog
Simon Willison's Weblog
宝玉的分享
宝玉的分享
PCI Perspectives
PCI Perspectives
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
I
InfoQ
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
S
Security @ Cisco Blogs
S
Schneier on Security
B
Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Cloudflare Blog
AWS News Blog
AWS News Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
H
Heimdal Security Blog
I
Intezer
A
Arctic Wolf
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
W
WeLiveSecurity

Oragekk's Blog

OpenSpec 技术分享:用规格驱动 AI 编程 ChatGPT&Codex订阅教程 一个 waline 评论系统bug引发的思考 Jenkins 远程触发构建踩坑记 Git SSH 密钥配置 初识Rust vuepress-plugin-meting2 谷歌发布多平台应用开发神器Project IDX!PaLM 2加持 Vue常见优化手段 Vue2响应式原理解析 Dart 中的并发 Flutter 工作原理 如何利用GitHub Action提交URL到搜索引擎 提交URL到搜索引擎(百度、Bing、Google) GitHub Actions 使用介绍 素材设计 前端-Q&A 浏览器的事件循环 你不知道的 CSS 之包含块 CSS 属性计算过程 Vercel deploy忽略指定分支 评论插件 Waline 之邮件通知配置 公开API 终端究极美化iTerm2+Pure 使用Bing API提交网站URL Flutter 基础大集合 关于本站 关于本站 关于我 Markdown 展示 使用n命令管理node版本 幻灯片页 ReactNative State(状态) ReactNative开发环境配置,ES6语法介绍 ReactNative介绍 更优雅强大的终端ZSH 神经网络模型训练 YYCache优秀的缓存设计 WebViewJavascriptBridge NSError WCDB漫谈 优雅的实现TableViewCell单选 初探机器学习框架CoreML 深入理解swift中闭包的捕捉语义 ijkPlayer 集成 iOS 配置https iOS timelineLogistics iOS Cookie的配置及使用 WKWebView拦截URL WKWebView使用及自适应高度 textfield限制输入字符 评论系统从多说迁移到disqus指南 利用Runtime进行快速归档 iOS 10.3 keychain 重大更新 通过UserAgent判断设备 AFNetworking A memory leak 一人一句宋词 OC 中的枚举类型 iOS - Image compression algorithm iOS程序启动原理(下) iOS程序启动原理(上) NSOperatioin TableView性能优化 Runloop Test Three ways to call Update Cocoapods 1.1.1 减小iOS-App或者静态库体积 Jekyll旧站回忆 JavaScript ES6 CommonJS,RequireJS,SeaJS 归纳笔记 Unix/Linux 扫盲笔记
Cell的accessoryType属性标记单元格之后,出现的重用问题
Oragekk · 2017-02-13 · via Oragekk's Blog

今天项目里出现一个问题,就是做一个列表选择,然后点击导航栏的确定按钮返回上级界面,并把选择的 cell 数据传递到上级界面。再使用 accessoryType 属性标记单元格之后会出现重用问题。

解决办法

  • 把 tableView 的 allowsMultipleSelection 属性设为了 YES;

    _tableView.allowsMultipleSelection = YES;
  • 在 didSelectRowAtIndexPath 和 didDeselectRowAtIndexPath 方法里面使用了如下方法实现了点击单元格然后用 check mark 标记的方式。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

	  cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

重点来了 两种思路

  • 记录选择的 indexpath
 	// 1.设一个NSMutableArray属性,元素个数跟你的_dataArray一样,初始化里面存的都是0。

	 NSMutableArray* selectionArray = [NSMutableArray array];
	 for (NSInteger i = 0; i < _dataArray.count; i++) {
    	[selectionArray addObject:@(0)]; // 0 表示未选中,1 表示选中了
	 }
	 self.selectionArray = selectionArray;

   // 2.在 didSelectRowAtIndexPath:里

	 [self.selectionArray replaceObjectAtIndex:indexPath.row withObject:@(1)];

	 [self.tableView reloadData];

   // 3.在 didDeselectRowAtIndexPath里:

	 [self.selectionArray replaceObjectAtIndex:indexPath.row withObject:@(0)];

	 [self.tableView reloadData];

   // 4.在 cellForRow里:
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
	cell.textLabel.text = _dataArray[indexPath.row];
	NSInteger selected = [self.selectionArray[indexPath.row] IntegerValue];
	if (selected == 0) {
    cell.accessoryType = UITableViewCellAccessoryNone;
	} else {
    	cell.accessoryType = UITableViewCellAccessoryCheckmark;
	}
	return cell;
  • 利用 cell 的 selected 属性

    • 继承 UITableViewCell,在 setSeleted:animated:方法里面,根据选择状态,修改 accessoryType
	 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    	 [super setSelected:selected animated:animated];
		 self.accessoryType = selected?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
		 // Configure the view for the selected state
	 }

	// 在 cellForRow里:
	cell.accessoryType = cell.selected?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;

至此已完美解决因为复用所导致的问题

	// 设置tableView可不可以选中
    self.tableView.allowsSelection = NO;

    // 允许tableview多选
    self.tableView.allowsMultipleSelection = YES;

    // 编辑模式下是否可以选中
    self.tableView.allowsSelectionDuringEditing = NO;

    // 编辑模式下是否可以多选
    self.tableView.allowsMultipleSelectionDuringEditing = YES;

    // 获取被选中的所有行
     [self.tableView indexPathsForSelectedRows]

    // 获取当前可见的行
     [self.tableView indexPathsForVisibleRows];