UITabView/UICollectionView 全选问题

UITabView/UICollectionView 全选问题

SkySeraph July. 30th 2016

Email:[email protected]

更多精彩请直接访问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

时间: 2024-10-12 22:50:57

UITabView/UICollectionView 全选问题的相关文章

jQuery学习(五)——使用JQ完成复选框的全选和全不选

1.在系统后台进行人员管理时,进行批量删除,使用jq完成全选和全不选 步骤分析: 第一步:引入jquery文件 第二步:书写页面加载函数 第三步:为上面的复选框绑定单击事件 第四步:将下面所有的复选框的选中状态设置成跟上面的一致! 2.具体代码实现: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>使用jQuery完成复选框的全选和全不

jq变态全选vs原生变态全选

<script> $(function(){ var num=0; $("#btn").on('click',function(){ if(this.checked){ $("#box").children().attr('checked',true); num=$("#box").children().length;//赋个值 }else{ $("#box").children().attr('checked',

JavaScript全选和取消全选

<html> <head> <title>全选和取消全选</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script language="javascript"> function checkAllorcancelAll(){ //思路:1.获取checkbox的选中状

关于input[type=&#39;checkbox&#39;]全选的问题

今天在做一个全选功能的时候,发现了一个问题,就是如果我在选择全选之前,我就已经选择了一个input,然后我再去选择全选并且以后再取消全选的时候,这个我之前选择的input始终处于选择状态,但是他的checked的值一直是在true和false之间变化,当checked=false的时候,仍然是被选中的.到现在还没处理好这个问题.希望看到的哪位大神能给个好的解决办法,实在感激.下面是类似的代码. <!DOCTYPE html><html lang="en"><

javascript实现全选,反选及全不选(其一)

javascript实现全选,反选及全部选 html 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <

Extjs 4.2 树结点右键菜单(全选,反选,撤销)

自己写的最新版本extjs4.2的树结点的操作,记录一下,以后可能会用到. var tree = new Ext.tree.TreePanel({ flex: 1, animate: true, autoScroll: true, anchor: '100% 93%', store: new Ext.data.TreeStore({ root:{ expanded: true, text:'A', children:[{ expanded: true, text:'1', children:[{

vim全选,全部复制,全部删除

全选(高亮显示):按esc后,然后ggvG或者ggVG 全部复制:按esc后,然后ggyG 全部删除:按esc后,然后dG 解析: gg:是让光标移到首行,在vim才有效,vi中无效 v : 是进入Visual(可视)模式 G :光标移到最后一行 选中内容以后就可以其他的操作了,比如: d  删除选中内容 y  复制选中内容到0号寄存器 "+y  复制选中内容到+寄存器,也就是系统的剪贴板,供其他程序用

购物车的全选,全不选,局部全选,全选判定

<!doctype html><html><head><meta charset="utf-8"><meta name="format-detection" content="telephone=no"/><meta name="viewport" content="user-scalable=no, width=device-width, initi

JavaScript内容梳理 示例之模态对话框 示例之全选和反选以及取消 示例之后台管理左侧菜单

1 <!DOCTYPE html> 2 <!--示例之模态对话框--> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Title</title> 7 <style> 8 .hide{ 9 display: none; 10 } 11 .c1{ 12 position: fixed; 13 lef