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

UITableView

UICollectionView

SYNC POST

========

By SkySeraph-2016  www.skyseraph.com

【iOS】UITabView/UICollectionView 全选问题的更多相关文章

  1. iOS单选和全选

    在日常开发中单选.多选.全选经常遇到,所以写一个demo放上来供大家参考, 先看效果图: Demo地址:https://github.com/domanc/SingleAndAllSelect.git

  2. IOS开发学习笔记029-反选、全选、删除按钮的实现

    还是在上一个程序的基础上进行修改 1.反选按钮 2.全选按钮 3.删除按钮 4.其他代码优化 1.反选按钮 反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_de ...

  3. iOS:iOS开发非常全的三方库、插件等等

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

  4. iOS的非常全的三方库,插件,大牛博客

    转自: http://www.cnblogs.com/zyjzyj/p/6015625.html github排名:https://github.com/trending, github搜索:http ...

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

    UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableVie ...

  6. BootStrapt iCheck表单美化插件使用方法详解(含参数、事件等) 全选 反选

    特色: 1.在不同浏览器(包括ie6+)和设备上都有相同的表现 — 包括 桌面和移动设备 2.支持触摸设备 — iOS.Android.BlackBerry.Windows Phone等系统 4.方便 ...

  7. jquery iCheck的全选和获取value

    jQuery iCheck 插件提供高度可定制的复选框和单选按钮(jQuery和Zepto).最新版本1.0.2,有个最新的2.0预览版,但是发布之后没有再更新. 特点:在不同的浏览器和设备(桌面和移 ...

  8. 移动端html5页面长按实现高亮全选文本内容的兼容解决方式

    近期须要给html5的WebAPP在页面上实现一个复制功能:用户点击长按文本会全选文字并弹出系统"复制"菜单.用户能够点击"复制"进行复制操作.然后粘贴到App ...

  9. Jquery的点击事件,三句代码完成全选事件

    先来看一下Js和Jquery的点击事件 举两个简单的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

随机推荐

  1. c语言一些知识点的记录

    1.extern关键字 extern关键字可以置于变量或者函数前面,用于告诉编译器此变量或函数定义于其他的模块.

  2. Python

    语法 #!/usr/bin/python 注释:# 编码:# -*- coding: UTF-8 -*- 缩进 运算符 算数运算符 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得 ...

  3. RabbitMQ学习

    参考链接:http://www.cnblogs.com/leocook/p/mq_rabbitmq_0.html

  4. Android中Context的理解及使用(一)——Context的作用

    Context的作用:用来访问全局信息的接口,通过Context进行资源的访问. 1.Context获取字符串资源: public class MainActivity extends AppComp ...

  5. [整理]AngularJS移动端开发遇到的问题

    最近在开发一个移动WebAPP的小项目,因为之前一直使用AngularJS, 对于这个项目,废话不多说,拿过来就用上了. 开发过程是一路通畅和舒服的,但是,却忽略了一个非常重要的问题,移动2G环境下的 ...

  6. frame和bounds

    - frame 是一个以**父视图**为坐标系的位置- bounds 是一个以**自身**为坐标系的位置- 如果改变了bounds 那么会影响子控件的显示位置

  7. Altium Designer 的entry sheet ,offsheet和port作用(转载)

    1.图纸结构 图纸包括两种结构关系: 一种是层次式图纸,该连接关系是纵向的,也就是某一层次的图纸只能和相邻的上级或下级有关系: 另一种是扁平式图纸,该连接关系是横向的,任何两张图纸之间都可以建立信号连 ...

  8. ASP.NET上实现

    ASP.NET上实现 fengzhuang.cs: using System;using System.Collections.Generic;using System.Linq;using Syst ...

  9. JSON Accelerator真是个好东西...

    支持OBJC,JAVA,Python,OBJC(Core Data),Python(Django) 并实现了NSCoding和NSCoping,方便归档和复制. 再也不用辛辛苦苦的写Model了.. ...

  10. 什么时候用Model,什么时候用Entity?

    在建立一个实体类的时候,究竟是用Model还是用Entity?比如MVC中,Model存了数据实体,但是他被称为Model,而在EF中,Entity也是存放数据实体,却被称作Entity,这两者有何区 ...