1. 程式人生 > >【iOS】UITabView/UICollectionView 全選問題

【iOS】UITabView/UICollectionView 全選問題

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

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

========  

相關推薦

iOSUITabView/UICollectionView 問題

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

iOS播放按鈕點音效

有點選按鈕產生音效的需求 /** 設定簽到音效 @param name 音效名稱 @param soundtype 音效型別 @param playtype 播放型別 */ -(void)playSoundWithName:(NSString *)name soundtype:

iOS拍照/相簿單、相簿多、圖片瀏覽、圖片裁剪

有一段時間沒有寫iOS系列的部落格了。最近由於專案的需要,就有封裝了一個拍照/相簿單選、相簿多選、圖片瀏覽、圖片裁剪的工具類。在封裝這這類是站在巨人的肩膀上做的。 1.ZLPhotoBrowser:https://github.com/longitachi/Z

iOSUICollectionView自己定義Layout之蜂窩布局

with top http reserve src 布局 step object .com 網上的UICollectionView的Layout布局,其cell的形狀多為矩形和圓形。 本篇博文將正六邊形作為cell的基本形狀,為您展現獨特的蜂窩布局效果及實現源代碼。 幫

javawebJQ實現複框的

需求:針對複選框,實現全選和全不選的操作 步驟分析:1. 匯入JQ的包                   2. 文件載入完成函式: 頁面初始化         &n

iOSButton點事件被其父檢視點手勢UITapGestureRecognizer的遮蔽問題

前幾天在做專案的時候,遇到這個一個問題,在一個檢視也就是UIView上新增一個手勢,然後又在這個View上新增一個UIButton,然後給按鈕新增事件,執行專案的時候我發現,不管是點選按鈕還是檢視上的別的地方執行的都是手勢所擁有的方法,後來到網上找才發現是手勢把按鈕的方法給

IOS實現IOS版的抽屜效果(點,拖拽滑動)

原文連結:http://blog.csdn.net/toss156/article/details/7400065 好像最近,看到好多Android上的抽屜效果,也忍不住想要自己寫一個。在Android裡面可以用SlidingDrawer,很方便的實現。IOS上面就只有自

iOSUITableView Cell自定義單功能

今天分享下cell的單選,自定義的,不是下圖這種網上找到的打對勾的,我搜了好久,基本上都是打對勾的文章,就決定自己寫一篇。基本上自己的app都會有一個風格吧,咱也不能一直用打對勾的方式去做(看起來是不是很low)。 我們要實現的是下面的這種形式。瞬間好看了

iOSUITbableView中cell的單狀態和多狀態

在之前的一篇《【iOS】UITableView中section的展開和收起》我們介紹了UITableView中section的展開和收起。當時的處理思路是給每一行的section一個狀態標記。在這一篇cell的單選也是才去的這樣一種處理思路。 思路: 1.初始的時候給每一個

iOS代理傳值與塊代碼傳值

ring 方法 nslog 設置代理 轉載 adf delegate alloc 代理傳值 主線程與子線程常常須要進行數據的傳遞。不同的類之間,不同的控制器之間都須要。 並且常常須要監聽一個動作的完畢。而後才去做對應事件。(代理是一對一的關系)。 一、代理傳值 代理

iOSUIDynamicAnimator動畫

set translate logs enc ica cgpoint isp mat .cn 創建動畫 1 UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:sel

轉載寫給省失利的童鞋們·沒有一個節點叫失敗

記得 不能 不難 tarjan google div 準備 童鞋 屏幕 我想我十八年來記憶最深刻的夜晚,大概便是在吉林大學燥熱的宿舍裏,穿著橘黃色的短袖,努力咽下大口大口的雪碧。那天晚上我仰躺在吉大熱得幾乎不能入眠的床板上,一動不動,看夏夜裏打不盡的蚊子在慘白的天花板下飛過

iOS淺拷貝和深拷貝

簡單 HR 除了 深淺拷貝 感謝 with 分配 指針 object 淺拷貝和深拷貝 淺拷貝即指針拷貝,可以認為是給一只狗加了一條繩子;深拷貝不但拷貝了對象的指針,還在系統中在分配了一塊內存用來存放拷貝對象的內容,可以認為是產生了一條帶繩子的新狗。即淺拷貝是兩根繩子一條狗

iOSJenkins持續集成iOS

edi 節點 path num nco visio os包 接下來 運行   今天項目經理說讓APP端做一下用Jenkins做持續集成,因為之前沒有做過,所以就開始了解,終於在鍵哥的幫助下,完成了持續集成的功能,感動的不行,在這裏感謝鍵哥。   因為公司的Jenkins是搭

Leetcode46. Permutations(排列)

out wap rem vat int ati swa problems pub 46. Permutations(全排列) 題目:鏈接 代碼一: class Solution { private: vector<vector<int>

iOS通過NSURLProtocol實現網頁載入本地快取資料

一.專案需求 專案中有個海報生成功能,使用UIWebView載入一些網頁,因為海報使用率比較高,有時載入網頁比較慢會影響使用者體驗,因此我們在APP啟動後,將一些固定資源,如css、圖片等,先快取到本地。載入網頁時,通過NSURLProtocol,優先使用本地的檔案,以加快網頁載入速

iOS中間透明的引導蒙層

需求 如圖口袋蜜蜂app一鍵海報的新手指引圖,需求是遮罩層中間透明的,把底層的第一張海報顯示出來,如圖: 實現 通過UIBezierPath和CAShapeLayer繪製一張中間為透明的黑色半透明遮罩層。 步奏1、新建類PCOnePosterGuide繼承自UIVie

iOS音訊播放之AVAudioPlayer,AVPlayer,AVQueuePlayer

前言 在婚語APP中,分別使用了AVAudioPlayer,AVPlayer,AVQueuePlayer來實現音訊播放功能,下面以婚語的實際需求分別介紹它們的使用方法和區別。 需求1 檔期備忘:使用者新建檔期記錄時,可以進行錄音備忘,錄音完成後可直接播放,儲存檔期時將錄音檔案上傳

iOS日曆行程的增刪改查(完整)

前言 我們可以使用系統提供的EventKit框架來訪問和操作使用者的日曆日程和提醒(雖然日曆和提醒是兩個獨立的app,但是是用同一個框架來處理資料)。同樣地,日曆和提醒的資料的資料,都是儲存在同一個叫做Calendar Database的資料庫中: EventKit不僅能獲

iOSUITableView中section的展開和收起

我們在做專案時,經常遇到UITableView需要展開與收起的情況,類似手機QQ的摺疊,而且根據資料的不同判斷有多少行需要展開與收起.之前做專案的時候也使用過,最近有時間整理,就把之前是怎麼操作的記錄下來。 首先,我們先說下思路: 思路:在寫程式碼的時候我們可以很容易的寫出cell和