UITabView/UICollectionView 全选问题

SkySeraph July. 30th 2016

Email:skyseraph00@163.com

更多精彩请直接访问SkySeraph个人站点:www.skyseraph.com

The Issue

Recently in my new project I need to select all the cell data in my UITabViewCell and UICollectionViewCell, and need to do some operations with all the cells(like delete etc.), What I do as follows:

UITabView

private func selectAll(select: Bool) {let numSections = mListTableView?.numberOfSectionsif let numSections = numSections {for numSection in  0 ..< numSections{let numItems = mListTableView?.numberOfRowsInSection(numSection)if let numItems = numItems {for numItem in 0 ..< numItems {selectCell(NSIndexPath(forRow: numItem, inSection: numSection), select: select)}}}}
}private func selectCell(indexPath : NSIndexPath, select: Bool) {if mListTableView?.cellForRowAtIndexPath(indexPath) != nil {let cell = mListTableView?.cellForRowAtIndexPath(indexPath) as! DownloadListViewCell//cell.setSelected(select, animated: true)cell.setSelectForDelete(select)  // select status UI in UITabViewCellmDownloadList[indexPath.row].selectToDelete = select  // Pojo data
    }
}

View Code

UICollectionView

private func selectAll(select: Bool) {let numSections = mMyOfflineCollectView?.numberOfSections()if let numSections = numSections {for numSection in  0 ..< numSections{let numItems = mMyOfflineCollectView?.numberOfItemsInSection(numSection)if let numItems = numItems {for numItem in 0 ..< numItems {selectCell(NSIndexPath(forRow: numItem, inSection: numSection), flag: select)}}}}
}private func selectCell(indexPath : NSIndexPath, flag: Bool) {if mMyOfflineCollectView.cellForItemAtIndexPath(indexPath) != nil {let cell = mMyOfflineCollectView.cellForItemAtIndexPath(indexPath) as! MyOfflineCollectionViewCellcell.setSelect(flag)if flag {mMyOfflineCollectView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.None)}else {mMyOfflineCollectView.deselectItemAtIndexPath(indexPath, animated: true)}mMyofflinesData[indexPath.row].needDelete = flag}
}

View Code

But, The problem is , I can only select the visible cell when I scoll down or up, or do operations

Solutions in NetWork

UICollectionView cellForItemAtIndexPath is nil

cellForItemAtIndexPath returns nil after force scrolling to make it visible

Select all the cells in UITableView

Easier way to select all rows in UITableView

tableView.cellForRowAtIndexPath returns nil with too many cells (swift)

tableView.cellForRowAtIndexPath(indexPath) return nil

The real Solution

The real problem happened at the cellForRowAtIndexPath / cellForItemAtIndexPath, Which defined in Apple as follows:

public func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?
// returns nil if cell is **not visible** or index path is out of range

public func cellForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewCell?

When the cell is not visible, the cellForRowAtIndexPath will return nil,
So, it’s not the right way to do the cell select operation out the
UITableViewDataSource in cellForRowAtIndexPath (UITabView), you should do it separate. The right way as follows:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {let cell = tableView.dequeueReusableCellWithIdentifier(DOWNLOAD_LIST_CELL_INDENTIFIER, forIndexPath: indexPath) as! DownloadListViewCellcell.selectionStyle = UITableViewCellSelectionStyle.None        // ...  cell.setSelectForDelete(self.mDownloadList[indexPath.row].selectToDelete)// select status UI in UITabViewCell// ...return cell
}private func selectCell(indexPath : NSIndexPath, select: Bool) {mDownloadList[indexPath.row].selectToDelete = select // Pojo datamListTableView?.reloadData() // reloadData
}

Ref

UITableView

UICollectionView

SYNC POST

========

By SkySeraph-2016  www.skyseraph.com

转载于:https://www.cnblogs.com/skyseraph/p/5652131.html

【iOS】UITabView/UICollectionView 全选问题相关推荐

  1. flutter 全选_Flutter ios 国际化(复制粘贴 中英文切换等问题)

    前提 在做flutter ios 国际化的时候遇到长按文本框崩溃的问题,然后google到一堆写法是重写cupertinoLocalization的奇怪做法,然后还千篇一律都是这么改的,其实不用那么麻 ...

  2. ios13全选手势_独家整理!20个iOS 13使用技巧和隐藏功能,让你的iPhone更好用

    iOS 13使用技巧汇总控制中心快速切换蓝牙设备和WiFi 以往控制中心只能允许用户开启/关闭WiFi或者蓝牙功能,而在iOS 13系统中,只需长按蓝牙.WiFi图标,即可快速打开对应的列表,并进行快 ...

  3. ios非常全的库iOS开发 非常全的三方库、插件、大牛博客等等

    转自: TimLiu-iOS Swift版本点击这里欢迎加入交QQ流群: 594119878 github排名 https://github.com/trending,github搜索:https:/ ...

  4. iOS:iOS开发非常全的三方库、插件、大牛博客等等

    iOS开发非常全的三方库.插件.大牛博客等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章 ...

  5. JavaScript基础12-day14【DOM查询(获取元素节点、获取元素节点的子节点)、全选练习、DOM增删改、DOM添加删除记录、Window对象方法】

    学习地址: 谷粒学院--尚硅谷 哔哩哔哩网站--尚硅谷最新版JavaScript基础全套教程完整版(140集实战教学,JS从入门到精通) JavaScript基础.高级学习笔记汇总表[尚硅谷最新版Ja ...

  6. iphone复制不能全选_iphone长按键盘进行复制、粘贴、全选操作技巧

    您可能感兴趣的话题: 苹果 核心提示:可能你已经不记得,但以前 iOS 并没有复制贴上功能.后来 Apple 终于想到一个方法将这功能放入 iOS -- 长按文字.这个方法的确十分简单易用,不过对于想 ...

  7. 模拟购物车页面全选单选的金额累加

    1.前面部分的内容就不过多说明了,就是OkGo请求数据然后封装适配器加入数据源,先上代码 先上适配器的代码,注意这里使用的万能适配器,加入依赖 implementation 'com.github.C ...

  8. iphone复制不能全选_IOS默认全选复制的三种方案

    类似于textField,长按就会默认跳出复制的选项,但是默认选中的就是你手指点击的那个文字,想全选的话必须手动选择全选才可以,而最近碰到一个需求就是要点击文字,默认就是选中所有的文字,比如游戏中的邀 ...

  9. 好用的下拉选框(单选,多选,全选,清空和反选,及个种样式)----个人钟爱

    下拉选框 如需样例和具体效果,请点击下面的连接. 好用的下拉选框(单选,多选,全选,清空和反选,及个种样式) 附件一:layui应用formselect layui.config({base: './ ...

最新文章

  1. [算法][递归] 棋盘覆盖
  2. 数字阅读体验的平台距离我们还有多远?
  3. 【PKUWC2018】Minimax【线段树合并】
  4. Linux下查看系统版本号信息的方法
  5. 一些难懂的笑话,看看你懂得多少? [转帖]
  6. python中range的用法_python的range怎么使用
  7. php将汉字转换为拼音和得到词语首字母(一)
  8. new Image().src资源重复请求问题
  9. 自制简易JTAG下载烧写工具
  10. PSP 版本6.60 破解 PRO-B10自制系统升级图文教程
  11. 论文必备:如何用卡片法写论文?
  12. 手把手教你制作一块Linux开发板(基于Planck-pi)
  13. Java+PDFBox将PDF转成图片
  14. 三星内存编码_三星内存铭牌的详细说明|三星记忆棒标签存储参数的详细说明...
  15. J - Janitor Troubles 三分 海伦公式
  16. parametrize参数化中使用skip标记
  17. MCS51延时程序分析
  18. TLS1.3 协议的加密过程
  19. 如何用python破解热点_Python程序员:没有我蹭不成功的网。教你如何愉快的蹭到WIFI?...
  20. Gym102870 2020-2021 “Orz Panda” Cup Programming Contest 补题记录

热门文章

  1. python Iterable
  2. mysql表空间转移_mysql共享表空间扩容,收缩,迁移
  3. 计算机系统结构自学试卷,全国2004年4月高等教育自学考试计算机系统结构试题...
  4. VMware 6 个少为人知的技巧
  5. Exchange Server 2016 独立部署/共存部署 (八)—— 边缘角色服务器
  6. VMware vSAN 的内部版本号和版本 (2150753)--2020-10-27 更新
  7. VMware产品支持倒计时
  8. Java基础学习总结(168)——为什么推荐在RPC的接口中入参和出参都不要使用枚举
  9. Docker学习总结(43)——Docker Compose 搭建Mysql主从复制集群
  10. Java基础学习总结(67)——Java接口API中使用数组的缺陷