博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【iOS】UITabView/UICollectionView 全选问题
阅读量:6974 次
发布时间:2019-06-27

本文共 3665 字,大约阅读时间需要 12 分钟。

July. 30th 2016

Email:

更多精彩请直接访问: 

 

 

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    }}
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! 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    }}
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

 

 

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 -2016   

 

转载地址:http://rhesl.baihongyu.com/

你可能感兴趣的文章
转 sql 优化
查看>>
PHP安全相关的配置(1)
查看>>
virtualbox 中,虚拟机网络使用NAT方式时,其它机器对虚拟机的访问
查看>>
Salesforce.com + AutoCAD WS集成研究 part2
查看>>
[Ubuntu] Access denied for user ‘debian-sys-maint’@'localhost’ (using password: YES)
查看>>
php curl请求转发
查看>>
设置 cell点击 背景色
查看>>
提高代码质量 CheckStyle FindBugs PMD
查看>>
【Java】HashTable和HashMap区别
查看>>
shell技巧之以逆序形式打印行
查看>>
Java面试题集(六)
查看>>
DCHP是什么意思
查看>>
go异常处理原则
查看>>
窗体传值
查看>>
跟我一起云计算(3)——hbase
查看>>
vim与外部文件的粘帖复制
查看>>
Entity Framework DBFirst尝试
查看>>
pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)
查看>>
【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)
查看>>
linux下启动oracle
查看>>