iOS專案開發實戰——Swift實現多個TableView的側滑與切換
在Android中我們常常使用ListView來表示列表,來顯示類似的呈現列表樣式的結果。來到iOS中,這種控制元件稱之為TableView。這裡我們將會通過使用ScrollView和TableView結合的方式來實現可以側滑顯示的列表,這將會大大提高使用者體驗。先看一下實現效果:
。
。
。
具體實現步驟如下:
(1)建立一個iOS專案,Language選擇Swift,然後在Main.storyboard中拖入一個ScrollView,即滾動控制元件,介面設計如圖:
。
(2)然後拖動控制元件繫結到程式碼中:
@IBOutlet weak var dynamicScrollView: UIScrollView!
(3)我將會在一個ScrollView中實現三個TableView,三個列表可以通過手指的左右滑動進行切換,一些變數定義如下:
var tableView11:UITableView = UITableView()
var tableView22:UITableView = UITableView()
var tableView33:UITableView = UITableView()
var cell1 = UITableViewCell()
var cell2 = UITableViewCell()
var cell3 = UITableViewCell()
(4)然後在viewDidLoad()中設定委託和資料來源,同時該類要實現以下介面:UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource
override func viewDidLoad() {
super.viewDidLoad()
tableView11.delegate = self
tableView11.dataSource = self
tableView22.delegate = self
tableView22.dataSource = self
tableView33.delegate = self
tableView33.dataSource = self
dynamicScroll()
initCustomTableView()
}
(5)實現dynamicScroll()方法,該方法是對ScrollView控制元件的滾動進行控制,同時把三個TableView加入到ScrollView中:
func dynamicScroll(){ //動態資訊的滾動;
let tableW:CGFloat = self.dynamicScrollView.frame.size.width;
let tableH:CGFloat = self.dynamicScrollView.frame.size.height;
var tableY:CGFloat = 0;
var totalCount:NSInteger = 3;//只有三列;
var tableView1:UITableView = UITableView();
var tableView2:UITableView = UITableView();
var tableView3:UITableView = UITableView();
tableView11.frame = CGRectMake(CGFloat(0) * tableW, tableY, tableW, tableH);
tableView22.frame = CGRectMake(CGFloat(1) * tableW, tableY, tableW, tableH);
tableView33.frame = CGRectMake(CGFloat(2) * tableW, tableY, tableW, tableH);
dynamicScrollView.addSubview(tableView11);
dynamicScrollView.addSubview(tableView22);
dynamicScrollView.addSubview(tableView33);
let contentW:CGFloat = tableW * CGFloat(totalCount);//這個表示整個ScrollView的長度;
dynamicScrollView.contentSize = CGSizeMake(contentW, 0);
dynamicScrollView.pagingEnabled = true;
dynamicScrollView.delegate = self;
}
(6)實現initCustomTableView()方法,該方法是對TableView的中的Cell設定ID號,用來標識不同的TableView :
func initCustomTableView(){ //初始化動態資訊中的TableView
tableView11.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell1")
tableView22.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell2")
tableView33.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell3")
}
(7)最後實現UITableViewDataSource中的兩個必須實現的方法,是對三個TableView的資料來源將進行設定:需要顯示的內容可以在這裡進行新增:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 5 //返回TableView的Cell數量,可以動態設定;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell()
switch tableView {
case tableView11:
cell1 = tableView11.dequeueReusableCellWithIdentifier("cell1") as! UITableViewCell
cell1.textLabel!.text = String(format:"昨天")
cell = cell1
break
case tableView22:
cell2 = tableView22.dequeueReusableCellWithIdentifier("cell2") as! UITableViewCell
cell2.textLabel!.text = String(format:"今天")
cell = cell2
break
case tableView33:
cell3 = tableView33.dequeueReusableCellWithIdentifier("cell3") as! UITableViewCell
cell3.textLabel!.text = String(format:"明天")
cell = cell3
break
default:
break
}
return cell
}
(8)最後執行程式,就可以實現本文開頭的多個TableView在ScrollView中通過側滑就可以切換的效果,雖然螢幕大小有限,我們可以通過檢視的切換顯示豐富的內容。
在iOS的開發中,TableView和ScrollView是兩個最為常用,使用最為靈活的控制元件,必須要好好掌握。
github主頁:https://github.com/chenyufeng1991 。歡迎大家訪問!
相關文章
- [MAUI]模仿iOS多工切換卡片滑動的互動實現UIiOS
- IOS多型別Cell的tableView實現iOS多型型別View
- 安卓開發:viewpager + fragment 實現滑動切換安卓ViewpagerFragment
- Flutter 仿iOS側滑返回案例實現FlutteriOS
- Flutter入門與實戰(五十一):Flutter多版本切換開發Flutter
- 實現Vue專案主題切換Vue
- iOS開發UI篇--一個側滑選單SlidingMenuiOSUI
- 專案要實現多資料來源動態切換,咋搞?
- Activity側滑返回的實現原理
- iOS開發之tableView左滑刪除的兩種方法iOSView
- CSS實現頁面切換時的滑動效果CSS
- Hack 蘋果系統 Api 實現 iOS TableViewCell 側滑方案蘋果APIiOSView
- 現有 Vue.js 專案快速實現多語言切換的一種思路Vue.js
- 小冊上新:Taro 多端開發實現原理與專案實戰
- 【iOS】含tableView的ViewController基類的實現iOSViewController
- iOS 多國語言本地化與App內語言切換(Swift)iOSAPPSwift
- 基於.NetCore開發部落格專案 StarBlog - (14) 實現主題切換功能NetCore
- flask 專案開發實戰Flask
- iOS 如何絲滑的側滑返回iOS
- 使用ViewPager和TabLayout來實現滑動切換效果ViewpagerTabLayout
- 實現一個切換配方的功能
- Python爬蟲開發與專案實戰pdfPython爬蟲
- Python爬蟲開發與專案實戰(2)Python爬蟲
- Python爬蟲開發與專案實戰(1)Python爬蟲
- ItemTouchHelper實現可拖拽和側滑的列表
- iOS開發- tableView的協議iOSView協議
- iOS開發——專案實戰總結&UITableView效能優化與卡頓問題iOSUIView優化
- 專案實戰介面開發SpringBootSpring Boot
- 短視訊平臺開發,依靠DrawerLayout實現側滑選單效果
- 一本實踐的專案開發《Python專案開發實戰(第2版)》Python
- iOS – RxSwift 專案實戰記錄iOSSwift
- Android 實現APP可切換多語言AndroidAPP
- python爬蟲實操專案_Python爬蟲開發與專案實戰 1.6 小結Python爬蟲
- 淺談設計模式在iOS開發實戰專案中的應用設計模式iOS
- 原生js實現一個側滑刪除取消元件(item slide)JS元件IDE
- Python爬蟲開發與專案實戰--分散式程式Python爬蟲分散式
- 小程式雲開發專案實戰
- django專案開發實戰——部落格Django
- 最新Python開發專案實戰(完整)Python