Swift實現(xiàn)表格視圖單元格單選(1)
本文實例為大家分享了Swift實現(xiàn)表格視圖單元格單選的具體代碼,供大家參考,具體內容如下
效果展示
前言
最近一個朋友問我,如何實現(xiàn)表格視圖的單選?因為我之前用Objective-c寫過一次,但那都是很久以前的事情了,于是就想著用swift實現(xiàn)一次,并分享給大家。
實現(xiàn)
下面我們來看看具體的實現(xiàn)方法。
首先我們創(chuàng)建一個Swift iOS工程,在AppDelegate.swift的didFinishLaunchingWithOptions
方法中手動初始化UIWindow,并且給根視圖控制器添加導航欄,當然在此之前我們需要到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文件中搭建界面,構造表格視圖,以及數(shù)據源的配置,這里我直接上代碼,相信表格視圖的使用大家已經非常熟悉了。代碼中注冊的單元格使用的是自定義的單元格,而處理單選的方法主要就是在自定義單元格CustomTableViewCell
中實現(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 = "單項選擇" ? ? ? ? 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") ? ? ? ? } ? ? } }
好了,表格視圖的單選就這樣實現(xiàn)了,運行看看吧??赡茉趯嶋H開發(fā)中會遇到更為復雜的情況,就是多個組的單選,比如你要做一個類似于習題庫的應用,將會用到多個組的單選,那應該如何實現(xiàn)呢?可參考:表格視圖單元格單選(二)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Swift利用CoreData實現(xiàn)一個上班簽到的小工具
這篇文章主要給大家介紹了關于Swift利用CoreData實現(xiàn)一個上班簽到小工具的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12用SwiftUI實現(xiàn)3D Scroll滾動效果的實現(xiàn)代碼
這篇文章主要介紹了用SwiftUI實現(xiàn)3D Scroll效果的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習2020-04-04swift3.0 創(chuàng)建sqlite數(shù)據庫步驟方法
本篇文章主要介紹了swift3.0 創(chuàng)建sqlite數(shù)據庫步驟方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06