Select all the cell in UITabView or UICollectionView problem

时间:2019-10-18
本文章向大家介绍Select all the cell in UITabView or UICollectionView problem,主要包括Select all the cell in UITabView or UICollectionView problem使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Catalogue

文章作者:SkySeraph
博客:skyseraph.com | GitHub | cnBlogs | 简书

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private func (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.setSelectForDelete(select) // select status UI in UITabViewCell
mDownloadList[indexPath.row].selectToDelete = select // Pojo data
}
}

UICollectionView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private func (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
}
}

大专栏  Select all the cell in UITabView or UICollectionView problemong>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:

1
2
public func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?
// returns nil if cell is **not visible** or index path is out of range
1
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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



By SkySeraph-2016

版权声明


SkySeraph by SkySeraph is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Bob创作并维护的SkySeraph博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证.
本文首发于SkySeraph博客( http://skyseraph.com ),版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

微信扫码打赏SkySeraph

如果您愿意捐助其它金额请戳我~~,扫码支付宝/微信

本文永久链接:http://skyseraph.com/2016/06/30/iOS/Select all the cell in UITabView or UICollectionView problem/

原文地址:https://www.cnblogs.com/dajunjun/p/11698665.html