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

前言
最近一個(gè)朋友問我,如何實(shí)現(xiàn)表格視圖的單選?因?yàn)槲抑坝肙bjective-c寫過一次,但那都是很久以前的事情了,于是就想著用swift實(shí)現(xiàn)一次,并分享給大家。
實(shí)現(xiàn)
下面我們來看看具體的實(shí)現(xiàn)方法。
首先我們創(chuàng)建一個(gè)Swift iOS工程,在AppDelegate.swift的didFinishLaunchingWithOptions方法中手動初始化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)非常熟悉了。代碼中注冊的單元格使用的是自定義的單元格,而處理單選的方法主要就是在自定義單元格CustomTableViewCell中實(shí)現(xiàn),稍后我會提及,涉及到的素材可到阿里矢量圖中下載。
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 = ["中國", "美國", "法國", "德國"]
? ? }
? ? 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 "請選擇您向往的城市:"
? ? }
? ? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
? ? ? ? // get cell with index path
? ? ? ? let cell = tableView.cellForRowAtIndexPath(indexPath)
? ? ? ? print("您選擇的城市:\((cell?.textLabel?.text)!)")
? ? }
}接下來我們看看 CustomTableViewCell.swift 文件,在自定義單元格中,要做的操作非常簡單,只需在 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í)際開發(fā)中會遇到更為復(fù)雜的情況,就是多個(gè)組的單選,比如你要做一個(gè)類似于習(xí)題庫的應(yīng)用,將會用到多個(gè)組的單選,那應(yīng)該如何實(shí)現(xiàn)呢?可參考:表格視圖單元格單選(二)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Swift 4中一些實(shí)用的數(shù)組技巧小結(jié)
這篇文章主要給大家分享了關(guān)于Swift 4中一些實(shí)用的數(shù)組技巧,文中通過示例代碼介紹的介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Swift實(shí)現(xiàn)復(fù)數(shù)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)復(fù)數(shù)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Swift利用CoreData實(shí)現(xiàn)一個(gè)上班簽到的小工具
這篇文章主要給大家介紹了關(guān)于Swift利用CoreData實(shí)現(xiàn)一個(gè)上班簽到小工具的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
用SwiftUI實(shí)現(xiàn)3D Scroll滾動效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了用SwiftUI實(shí)現(xiàn)3D Scroll效果的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)2020-04-04
swift3.0 創(chuàng)建sqlite數(shù)據(jù)庫步驟方法
本篇文章主要介紹了swift3.0 創(chuàng)建sqlite數(shù)據(jù)庫步驟方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

