【iOS】UITabView/UICollectionView 全选问题
2016-07-08 08:22
471 查看
UITabView/UICollectionView 全选问题
SkySeraph July. 30th 2016Email: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 = 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 nilcellForItemAtIndexPath 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
UITableViewUICollectionView
SYNC POST
========
By SkySeraph-2016 www.skyseraph.com
相关文章推荐
- Implement Queue using Stacks
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"
- HDU 5531(Rebuild- 三分)
- Longest Increasing Subsequence
- Gradle version 2.20 is required. Current version is 2.10 解决方法
- Vue.js学习 Item8 -- 方法与事件处理器
- mui框架如何实现页面间传值
- Vue.js学习 Item7 -- 条件渲染与列表渲染
- UITableViewCell重用时遇到的问题
- Vue.js学习 Item6 -- Class 与 样式绑定
- Data Import Request Handler
- weui
- Vue.js学习 Item5 -- 计算属性computed与$watch
- Windows平板装Android双系统及建立UEFI引导全攻略(windows8.1+android6+phoenix)
- Vue.js学习 Item4 -- 数据双向绑定
- query term weight计算
- 使用hebernate SQLQuery执行原生SQL
- MUI开发注意事项
- Vue.js学习 Item3 -- 构造器与生命周期
- UIScrollView contentInset 属性详解