UITabView/UICollectionView 全选问题
来源:互联网 发布:手机如何连接电脑网络 编辑:程序博客网 时间:2024/04/30 08:25
更多精彩请直接访问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
- UITabView/UICollectionView 全选问题
- UITabView
- UICollectionView的横线问题
- iOS:UICollectionView布局问题
- iphone UItabview
- UICollectionView重用机制的问题
- UICollectionView的下拉刷新问题
- QA:UICollectionView不能选中问题
- UICollectionView 多选显示问题
- UICollectionView 与屏幕旋转 问题
- UICollectionView的cell间距问题
- js全选问题
- Repeater/CheckBox 全选问题
- girdview全选问题
- GridView 全选问题
- checkbox 全选的问题
- 解决checkbox全选之后取消全选,再次全选无效问题
- UICollectionView
- APP跳到AppStore 下载的实现
- linux vlan configuration
- python爬虫1
- UML类图建模简介
- java学习路线图
- UITabView/UICollectionView 全选问题
- XSS实战篇
- Android Studio下开启手机无线调试
- JAVA枚举的作用与好处
- Java环境的搭建
- 让您的Xcode键字如飞
- [Cloud Computing]Mechanisms: Cloud Usage Monitor
- nodejs:Excel导出json
- C++匿名命名空间