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

前言
最近一個朋友問我,如何實現(xiàn)表格視圖的單選?因為我之前用Objective-c寫過一次,但那都是很久以前的事情了,于是就想著用swift實現(xiàn)一次,并分享給大家。
實現(xiàn)
下面我們來看看具體的實現(xiàn)方法。
首先我們創(chuàng)建一個Swift iOS工程,在AppDelegate.swift的didFinishLaunchingWithOptions方法中手動初始化UIWindow,并且給根視圖控制器添加導(dǎo)航欄,當然在此之前我們需要到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中實現(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)了,運行看看吧。可能在實際開發(fā)中會遇到更為復(fù)雜的情況,就是多個組的單選,比如你要做一個類似于習(xí)題庫的應(yīng)用,將會用到多個組的單選,那應(yīng)該如何實現(xiàn)呢?可參考:表格視圖單元格單選(二)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何利用SwiftUI實現(xiàn)可縮放的圖片預(yù)覽器
這篇文章主要給大家介紹了關(guān)于如何利用SwiftUI實現(xiàn)可縮放圖片預(yù)覽器的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用SwiftUI具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-09-09
Spring中BeanFactory與FactoryBean的區(qū)別解讀
這篇文章主要介紹了Spring中BeanFactory與FactoryBean的區(qū)別解讀,Java的BeanFactory是Spring框架中的一個接口,它是用來管理和創(chuàng)建對象的工廠接口,在Spring中,我們可以定義多個BeanFactory來管理不同的組件,需要的朋友可以參考下2023-12-12
Swift如何調(diào)用Objective-C的可變參數(shù)函數(shù)詳解
這篇文章主要給大家介紹了關(guān)于Swift如何調(diào)用Objective-C的可變參數(shù)函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用swift具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03

