国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

Swift實現(xiàn)多個TableView側(cè)滑與切換效果

2020-03-09 17:43:16
字體:
供稿:網(wǎng)友

在Android中我們常常使用ListView來表示列表,來顯示類似的呈現(xiàn)列表樣式的結(jié)果。來到iOS中,這種控件稱之為TableView。這里我們將會通過使用ScrollView和TableView結(jié)合的方式來實現(xiàn)可以側(cè)滑顯示的列表,這將會大大提高用戶體驗。先看一下實現(xiàn)效果:

Swift,TableView,側(cè)滑,切換

Swift,TableView,側(cè)滑,切換

 

Swift,TableView,側(cè)滑,切換

具體實現(xiàn)步驟如下:

(1)創(chuàng)建一個iOS項目,Language選擇Swift,然后在Main.storyboard中拖入一個ScrollView,即滾動控件,界面設(shè)計如圖:

Swift,TableView,側(cè)滑,切換

(2)然后拖動控件綁定到代碼中:

@IBOutlet weak var dynamicScrollView: UIScrollView! 

(3)我將會在一個ScrollView中實現(xiàn)三個TableView,三個列表可以通過手指的左右滑動進(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ù)源,同時該類要實現(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)實現(xiàn)dynamicScroll()方法,該方法是對ScrollView控件的滾動進(jìn)行控制,同時把三個TableView加入到ScrollView中:

func dynamicScroll(){ //動態(tài)信息的滾動;   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)實現(xiàn)initCustomTableView()方法,該方法是對TableView的中的Cell設(shè)置ID號,用來標(biāo)識不同的TableView :

func initCustomTableView(){  //初始化動態(tài)信息中的TableView      tableView11.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell1")   tableView22.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell2")   tableView33.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell3") } 

(7)最后實現(xiàn)UITableViewDataSource中的兩個必須實現(xiàn)的方法,是對三個TableView的數(shù)據(jù)源將進(jìn)行設(shè)置:需要顯示的內(nèi)容可以在這里進(jìn)行添加:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{   return 5 //返回TableView的Cell數(shù)量,可以動態(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)最后運行程序,就可以實現(xiàn)本文開頭的多個TableView在ScrollView中通過側(cè)滑就可以切換的效果,雖然屏幕大小有限,我們可以通過視圖的切換顯示豐富的內(nèi)容。

在iOS的開發(fā)中,TableView和ScrollView是兩個最為常用,使用最為靈活的控件,必須要好好掌握。

github主頁:https://github.com/chenyufeng1991  。歡迎大家訪問!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到swift教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝鸡市| 郸城县| 东阳市| 合川市| 叙永县| 遵义市| 松江区| 拉孜县| 乌鲁木齐县| 谷城县| 枣强县| 家居| 莱芜市| 马尔康县| 斗六市| 无为县| 五河县| 桃园市| 江永县| 无为县| 尤溪县| 韩城市| 象山县| 葵青区| 宁武县| 盐边县| 永登县| 辉南县| 连州市| 霍城县| 靖边县| 盐池县| 大埔区| 湘潭县| 都安| 德阳市| 广河县| 六盘水市| 深水埗区| 彰武县| 遂昌县|