Fork me on GitHub

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

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 

 

posted @   SkySeraph  阅读(1375)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示