UITabView/UICollectionView 全选问题

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值