99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
黑暗掠奪者 頭像
蘋果2袋
2
黑暗掠奪者

Cell中添加Switch組件如何執(zhí)行不同的函數(shù)

發(fā)布時(shí)間:2018-02-25 20:07  回復(fù):0  查看:2872   最后回復(fù):2018-02-25 20:07  

OneSwift - iOS Tips Based On Swift

Switch組件頻繁使用于用戶設(shè)置、自定義等場(chǎng)景,當(dāng)Switch組件只有一個(gè)時(shí),它的數(shù)據(jù)綁定、函數(shù)綁定和狀態(tài)切換都相對(duì)簡單,只需要對(duì)一個(gè)Switch進(jìn)行操作即可。

但是,當(dāng)我們?cè)?code>TableView和CollectionView中復(fù)用多個(gè)Switch時(shí),就需要一個(gè)更好的方式去同時(shí)實(shí)現(xiàn)多個(gè)數(shù)據(jù)綁定、函數(shù)綁定和狀態(tài)切換。

在這里先說明一下:

數(shù)據(jù)綁定是指需要判斷當(dāng)前Switch的初始值狀態(tài); 函數(shù)綁定是指點(diǎn)擊不同的Switch執(zhí)行不同的函數(shù); 狀態(tài)切換是指點(diǎn)擊之后我們需要改變Switch的狀態(tài);

下面以CollectionView中使用多個(gè)Switch為例,進(jìn)行實(shí)踐演練。

1.創(chuàng)建Switch組件

創(chuàng)建Switch組件,設(shè)置identifyCellSwitch


CellSwitch

2.區(qū)分與標(biāo)識(shí)Switch

通過在cellForItemAt中設(shè)置Switch的不同tag值,來區(qū)分Switch 這里我以

cell.CellSwitch.tag = indexPath.row

3.Switch初始化狀態(tài)加載

cellForItemAt中加載Switch的各種顏色和當(dāng)前狀態(tài)。 當(dāng)然要明確不同的indexPath.row對(duì)應(yīng)什么值,對(duì)應(yīng)的Switch加載不同的初始狀態(tài),以O(shè)neClock自定義設(shè)置中的三個(gè)值為例,我調(diào)用coredata的值進(jìn)行了初始化狀態(tài)的加載。

switch indexPath.row {
        case 0:
            print("indexpath",indexPath.row)
            if self.appDelegate.mytheme.timeFormat == Int64(0){
                cell.CellSwitch.isOn = true
            }else{
                cell.CellSwitch.isOn = false
            }


        case 1:
            print("indexpath",indexPath.row)
            if self.appDelegate.mytheme.showFormat == Int64(0){
                cell.CellSwitch.isOn = true
            }else{
                cell.CellSwitch.isOn = false
            }
        case 2:
            print("indexpath",indexPath.row)
            if self.appDelegate.mytheme.oneClickMenu == Int64(0){
                cell.CellSwitch.isOn = true
            }else{
                cell.CellSwitch.isOn = false
            }
        default:
            print("default")
        }

4.Switch函數(shù)綁定

同樣在cellForItemAt中加載Switch的函數(shù)self.switchAction(_ :),代碼如下:

cell.CellSwitch.addTarget(self, action: #selector(self.switchAction(_ :)), for: .touchUpInside)
當(dāng)然,這里的核心是 self.switchAction(_ :) 函數(shù)本身, 剛剛我們已經(jīng)給每個(gè)Switch加了tag,因此在這個(gè)函數(shù)中,我們就只需要判斷tag值進(jìn)行不同的函數(shù)操作即可。
@objc func switchAction(_ sender: UISwitch){
        switch sender.tag {
        case 0:
            print("indexpath",sender.tag)
            if self.appDelegate.mytheme.timeFormat == Int64(0){
               self.appDelegate.mytheme.timeFormat = Int64(1)
            }else{
               self.appDelegate.mytheme.timeFormat = Int64(0)
            }

        case 1:
            print("indexpath",sender.tag)
            if self.appDelegate.mytheme.showFormat == Int64(0){
                self.appDelegate.mytheme.showFormat = Int64(1)
            }else{
                self.appDelegate.mytheme.showFormat = Int64(0)
            }
        case 2:
            print("indexpath",sender.tag)
            if self.appDelegate.mytheme.oneClickMenu == Int64(0){
                self.appDelegate.mytheme.oneClickMenu = Int64(1)
            }else{
                self.appDelegate.mytheme.oneClickMenu = Int64(0)
            }
        default:
            print("default")
        }
    }

5.改變Switch狀態(tài)

只要初始狀態(tài)和函數(shù)改變的方式統(tǒng)一,每次點(diǎn)擊都能獲得正確的狀態(tài)和值的改變,也可以在每次執(zhí)行self.switchAction(_ :)時(shí),加入整個(gè)CollectionView的重置:

self.CustomCollection.reload()

最后看看效果

多個(gè)Switch演示


GitHub:OneSwift - iOS Tips Based On Swift

微博:xDEHANG



您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?