Swift實(shí)現(xiàn)多個(gè)TableView側(cè)滑與切換效果
在Android中我們常常使用ListView來(lái)表示列表,來(lái)顯示類似的呈現(xiàn)列表樣式的結(jié)果。來(lái)到iOS中,這種控件稱之為T(mén)ableView。這里我們將會(huì)通過(guò)使用ScrollView和TableView結(jié)合的方式來(lái)實(shí)現(xiàn)可以側(cè)滑顯示的列表,這將會(huì)大大提高用戶體驗(yàn)。先看一下實(shí)現(xiàn)效果:
。
。
。
具體實(shí)現(xiàn)步驟如下:
(1)創(chuàng)建一個(gè)iOS項(xiàng)目,Language選擇Swift,然后在Main.storyboard中拖入一個(gè)ScrollView,即滾動(dòng)控件,界面設(shè)計(jì)如圖:
。
(2)然后拖動(dòng)控件綁定到代碼中:
@IBOutlet weak var dynamicScrollView: UIScrollView!
(3)我將會(huì)在一個(gè)ScrollView中實(shí)現(xiàn)三個(gè)TableView,三個(gè)列表可以通過(guò)手指的左右滑動(dòng)進(jìn)行切換,一些變量定義如下:
var tableView11:UITableView = UITableView() var tableView22:UITableView = UITableView() var tableView33:UITableView = UITableView() var cell1 = UITableViewCell() var cell2 = UITableViewCell() var cell3 = UITableViewCell()
(4)然后在viewDidLoad()中設(shè)置委托和數(shù)據(jù)源,同時(shí)該類要實(shí)現(xiàn)以下接口: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)實(shí)現(xiàn)dynamicScroll()方法,該方法是對(duì)ScrollView控件的滾動(dòng)進(jìn)行控制,同時(shí)把三個(gè)TableView加入到ScrollView中:
func dynamicScroll(){ //動(dòng)態(tài)信息的滾動(dòng);
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);//這個(gè)表示整個(gè)ScrollView的長(zhǎng)度;
dynamicScrollView.contentSize = CGSizeMake(contentW, 0);
dynamicScrollView.pagingEnabled = true;
dynamicScrollView.delegate = self;
}
(6)實(shí)現(xiàn)initCustomTableView()方法,該方法是對(duì)TableView的中的Cell設(shè)置ID號(hào),用來(lái)標(biāo)識(shí)不同的TableView :
func initCustomTableView(){ //初始化動(dòng)態(tài)信息中的TableView
tableView11.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell1")
tableView22.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell2")
tableView33.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell3")
}
(7)最后實(shí)現(xiàn)UITableViewDataSource中的兩個(gè)必須實(shí)現(xiàn)的方法,是對(duì)三個(gè)TableView的數(shù)據(jù)源將進(jìn)行設(shè)置:需要顯示的內(nèi)容可以在這里進(jìn)行添加:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 5 //返回TableView的Cell數(shù)量,可以動(dòng)態(tài)設(shè)置;
}
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)最后運(yùn)行程序,就可以實(shí)現(xiàn)本文開(kāi)頭的多個(gè)TableView在ScrollView中通過(guò)側(cè)滑就可以切換的效果,雖然屏幕大小有限,我們可以通過(guò)視圖的切換顯示豐富的內(nèi)容。
在iOS的開(kāi)發(fā)中,TableView和ScrollView是兩個(gè)最為常用,使用最為靈活的控件,必須要好好掌握。
github主頁(yè):https://github.com/chenyufeng1991 。歡迎大家訪問(wèn)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot3.0集成Redis緩存的實(shí)現(xiàn)示例
緩存就是一個(gè)存儲(chǔ)器,常用 Redis作為緩存數(shù)據(jù)庫(kù),本文主要介紹了SpringBoot3.0集成Redis緩存的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Swift自動(dòng)調(diào)整視圖布局AutoLayout和AutoresizingMask功能詳解
這篇文章主要為大家介紹了Swift自動(dòng)調(diào)整視圖布局AutoLayout和AutoresizingMask功能及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Swift中static和class關(guān)鍵字的深入講解
這篇文章主要給大家介紹了關(guān)于Swift中static和class關(guān)鍵字的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Swift的開(kāi)發(fā)環(huán)境搭建以及基本語(yǔ)法詳解
這篇文章主要介紹了Swift的開(kāi)發(fā)環(huán)境搭建以及基本語(yǔ)法詳解,是Swift入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11
Swift類型創(chuàng)建之自定義一個(gè)類型詳解
這篇文章主要介紹了Swift類型創(chuàng)建之自定義一個(gè)類型詳解,本文講解了自定義原型、實(shí)現(xiàn)默認(rèn)值、支持基本布爾型初始化、支持Bool類型判斷、支持兼容各們各派的類型、完善OCBool的布爾基因體系等內(nèi)容,需要的朋友可以參考下2015-05-05
詳解在swift中實(shí)現(xiàn)NSCoding的自動(dòng)歸檔和解檔
本篇文章主要介紹了在swift中實(shí)現(xiàn)NSCoding的自動(dòng)歸檔和解檔,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

