【iOS】UITabView/UICollectionView 全选问题
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?.numberOfSections
if 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 UITabViewCell
mDownloadList[indexPath.row].selectToDelete = select // Pojo data
}
}
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! MyOfflineCollectionViewCell
cell.setSelect(flag)
if flag {
mMyOfflineCollectView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.None)
}else {
mMyOfflineCollectView.deselectItemAtIndexPath(indexPath, animated: true)
}
mMyofflinesData[indexPath.row].needDelete = flag
}
}
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! DownloadListViewCell
cell.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 data
mListTableView?.reloadData() // reloadData
}
Ref
========
By SkySeraph-2016 www.skyseraph.com
【iOS】UITabView/UICollectionView 全选问题的更多相关文章
- iOS单选和全选
在日常开发中单选.多选.全选经常遇到,所以写一个demo放上来供大家参考, 先看效果图: Demo地址:https://github.com/domanc/SingleAndAllSelect.git
- IOS开发学习笔记029-反选、全选、删除按钮的实现
还是在上一个程序的基础上进行修改 1.反选按钮 2.全选按钮 3.删除按钮 4.其他代码优化 1.反选按钮 反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_de ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS的非常全的三方库,插件,大牛博客
转自: http://www.cnblogs.com/zyjzyj/p/6015625.html github排名:https://github.com/trending, github搜索:http ...
- iOS开发 非常全的三方库、插件、大牛博客等等
UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableVie ...
- BootStrapt iCheck表单美化插件使用方法详解(含参数、事件等) 全选 反选
特色: 1.在不同浏览器(包括ie6+)和设备上都有相同的表现 — 包括 桌面和移动设备 2.支持触摸设备 — iOS.Android.BlackBerry.Windows Phone等系统 4.方便 ...
- jquery iCheck的全选和获取value
jQuery iCheck 插件提供高度可定制的复选框和单选按钮(jQuery和Zepto).最新版本1.0.2,有个最新的2.0预览版,但是发布之后没有再更新. 特点:在不同的浏览器和设备(桌面和移 ...
- 移动端html5页面长按实现高亮全选文本内容的兼容解决方式
近期须要给html5的WebAPP在页面上实现一个复制功能:用户点击长按文本会全选文字并弹出系统"复制"菜单.用户能够点击"复制"进行复制操作.然后粘贴到App ...
- Jquery的点击事件,三句代码完成全选事件
先来看一下Js和Jquery的点击事件 举两个简单的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
随机推荐
- 关于编写性能高效的javascript事件的技术
如何能做出高效的web前端程序是我每次做前端开发都会不自觉去考虑的问题.几年前雅虎里牛逼的前端工程师们出了一本关于提升web前端性能的书籍,轰动了整个web开发技术界,让神秘的web前端优化问题成为了 ...
- Mysql基础(二)
学习路线:数据约束-> 数据库的设计过程-> 存储过程的相关知识-> 触发器-> 权限管理 (一)数据约束 1.1.默认值的设置 创建员工表emp 将默认地址设置为'中国'my ...
- python网络编程学习笔记(三):socket网络服务器(转载)
1.TCP连接的建立方法 客户端在建立一个TCP连接时一般需要两步,而服务器的这个过程需要四步,具体见下面的比较. 步骤 TCP客户端 TCP服务器 第一步 建立socket对象 建立socket对 ...
- 解决svn pritine text not exist问题
svn: E155032: The pristine text with checksum '$sha1$151400d1cd4c5fc190d500aa1826d45cb91f088f' not f ...
- 【学习笔记】Servlet的配置
为了让Servlet能响应用户的请求,需要将Servlet配置在Web应用中. 从Servlet3.0开始,配置Servlet有两种方式: l 在Servlet类中使用@WebServlet Anno ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- 如何安装 第三方 Go 离线包? (GOPATH、 go install)
有时候 go get 比较慢,可以考虑用迅雷等下载工具下载下来,然后再本地安装, 如:code.google.com/p/go.net/websocket,如何安装这些离线包? 先在你的 GOPATH ...
- MySQL复制和集群
一.复制配置 (A) 主从服务器相同版本的数据库 (B) 主服务器上复制使用的账户,具有相应的权限. (C) 修改主服务器的配置文件my.cnf,开启BINLOG,并设置server-id的值.重启后 ...
- 手机设计尺寸 - iPhone界面尺寸
参考网址: http://www.qijishow.com/down/app-index.htm iPhone界面尺寸 设备 分辨率 PPI 状态栏高度 导航栏高度 标签栏高度 iPhone6 plu ...
- RSA非对称性前端加密后端解密
前端加密代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...