Swift實(shí)現(xiàn)表格視圖單元格單選(1)
本文實(shí)例為大家分享了Swift實(shí)現(xiàn)表格視圖單元格單選的具體代碼,供大家參考,具體內(nèi)容如下
效果展示

前言
最近一個(gè)朋友問(wèn)我,如何實(shí)現(xiàn)表格視圖的單選?因?yàn)槲抑坝肙bjective-c寫(xiě)過(guò)一次,但那都是很久以前的事情了,于是就想著用swift實(shí)現(xiàn)一次,并分享給大家。
實(shí)現(xiàn)
下面我們來(lái)看看具體的實(shí)現(xiàn)方法。
首先我們創(chuàng)建一個(gè)Swift iOS工程,在AppDelegate.swift的didFinishLaunchingWithOptions方法中手動(dòng)初始化UIWindow,并且給根視圖控制器添加導(dǎo)航欄,當(dāng)然在此之前我們需要到Info.plist文件中將Storyboard加載UIWindow字段的Main值刪除。
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.backgroundColor = UIColor.blackColor() self.window?.rootViewController = UINavigationController(rootViewController: ViewController()) self.window?.makeKeyAndVisible()
下一步,我們需要到ViewController.swift文件中搭建界面,構(gòu)造表格視圖,以及數(shù)據(jù)源的配置,這里我直接上代碼,相信表格視圖的使用大家已經(jīng)非常熟悉了。代碼中注冊(cè)的單元格使用的是自定義的單元格,而處理單選的方法主要就是在自定義單元格CustomTableViewCell中實(shí)現(xiàn),稍后我會(huì)提及,涉及到的素材可到阿里矢量圖中下載。
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
? ? var tableView: UITableView?
? ? var dataSource: [String]?
? ? override func viewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? self.initializeDatasource()
? ? ? ? self.initializeUserInterface()
? ? }
? ? // MARK:Initialize methods
? ? func initializeDatasource() {
? ? ? ? self.dataSource = ["中國(guó)", "美國(guó)", "法國(guó)", "德國(guó)"]
? ? }
? ? func initializeUserInterface() {
? ? ? ? self.title = "單項(xiàng)選擇"
? ? ? ? self.automaticallyAdjustsScrollViewInsets = false
? ? ? ? self.view.backgroundColor = UIColor.whiteColor()
? ? ? ? // initialize table view
? ? ? ? self.tableView = {
? ? ? ? ? ? let tableView = UITableView(frame: CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64), style: UITableViewStyle.Grouped)
? ? ? ? ? ? tableView.dataSource = self
? ? ? ? ? ? tableView.delegate = self
? ? ? ? ? ? tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
? ? ? ? ? ? tableView.registerClass(CustomTableViewCell.classForCoder(), forCellReuseIdentifier: "cellIdentifier")
? ? ? ? ? ? return tableView
? ? ? ? ? ? }()
? ? ? ? self.view.addSubview(self.tableView!)
? ? }
? ? // MARK:UITableViewDataSource && UITableViewDelegate
? ? func numberOfSectionsInTableView(tableView: UITableView) -> Int {
? ? ? ? return 1
? ? }
? ? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
? ? ? ? return (self.dataSource?.count)!
? ? }
? ? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
? ? ? ? let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! CustomTableViewCell
? ? ? ? cell.imageView?.image = UIImage(named: "iconfont-select.png")
? ? ? ? cell.textLabel?.text = self.dataSource![indexPath.row]
? ? ? ? return cell
? ? }
? ? func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
? ? ? ? return 40
? ? }
? ? func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
? ? ? ? return "請(qǐng)選擇您向往的城市:"
? ? }
? ? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
? ? ? ? // get cell with index path
? ? ? ? let cell = tableView.cellForRowAtIndexPath(indexPath)
? ? ? ? print("您選擇的城市:\((cell?.textLabel?.text)!)")
? ? }
}接下來(lái)我們看看 CustomTableViewCell.swift 文件,在自定義單元格中,要做的操作非常簡(jiǎn)單,只需在 setSelected方法中做如下操作即可:
override func setSelected(selected: Bool, animated: Bool) {
? ? ? ? super.setSelected(selected, animated: animated)
? ? ? ? if selected {
? ? ? ? ? ? imageView?.image = UIImage(named: "iconfont-selected.png")
? ? ? ? }else {
? ? ? ? ? ? imageView?.image = UIImage(named: "iconfont-select.png")
? ? ? ? }
? ? }
}好了,表格視圖的單選就這樣實(shí)現(xiàn)了,運(yùn)行看看吧??赡茉趯?shí)際開(kāi)發(fā)中會(huì)遇到更為復(fù)雜的情況,就是多個(gè)組的單選,比如你要做一個(gè)類(lèi)似于習(xí)題庫(kù)的應(yīng)用,將會(huì)用到多個(gè)組的單選,那應(yīng)該如何實(shí)現(xiàn)呢?可參考:表格視圖單元格單選(二)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何利用SwiftUI實(shí)現(xiàn)可縮放的圖片預(yù)覽器
這篇文章主要給大家介紹了關(guān)于如何利用SwiftUI實(shí)現(xiàn)可縮放圖片預(yù)覽器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SwiftUI具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
Spring中BeanFactory與FactoryBean的區(qū)別解讀
這篇文章主要介紹了Spring中BeanFactory與FactoryBean的區(qū)別解讀,Java的BeanFactory是Spring框架中的一個(gè)接口,它是用來(lái)管理和創(chuàng)建對(duì)象的工廠接口,在Spring中,我們可以定義多個(gè)BeanFactory來(lái)管理不同的組件,需要的朋友可以參考下2023-12-12
Swift如何調(diào)用Objective-C的可變參數(shù)函數(shù)詳解
這篇文章主要給大家介紹了關(guān)于Swift如何調(diào)用Objective-C的可變參數(shù)函數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Swift實(shí)現(xiàn)“或”操作符的3種方法示例
這篇文章主要給大家介紹了關(guān)于Swift實(shí)現(xiàn)“或”操作符的3種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
在?Swift?中編寫(xiě)Git?Hooks腳本的方法
在本例中,我使用了?commit-msg?鉤子,它能夠在當(dāng)前提交信息生效前修改此信息,鉤子由一個(gè)參數(shù)調(diào)用,該參數(shù)是指向包含用戶輸入的提交消息的文件的路徑,這意味著,為了改變提交消息,我們只需要從文件中讀取、修改其內(nèi)容,然后寫(xiě)回調(diào)用掛鉤的文件2022-06-06
Swift編程中實(shí)現(xiàn)希爾排序算法的代碼實(shí)例
希爾排序是對(duì)插入排序的一種改進(jìn)版本,算法本身并不穩(wěn)定,存在優(yōu)化空間,這里我們來(lái)講一下希爾排序的大體思路及Swift編程中實(shí)現(xiàn)希爾排序算法的代碼實(shí)例2016-07-07
iOS UITableView展開(kāi)縮放動(dòng)畫(huà)實(shí)例代碼
這篇文章主要介紹了Swift UITableView展開(kāi)縮放動(dòng)畫(huà)實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08

