Swift初探
?
問(wèn)題描述:
如上圖這是一個(gè)根導(dǎo)航器 ,里面包含多個(gè)section,和row ,根據(jù)用戶點(diǎn)擊的cell ,進(jìn)入相應(yīng)的頁(yè)面,由于每個(gè)頁(yè)面的初始化都不一樣 ,所以大部分的處理都會(huì)寫(xiě)成這樣:
swtich(index.section) {
?? swtich:(index.row) {
//做相應(yīng)處理
}
}
如果section 和row 比較多 也起來(lái)會(huì)很煩,而且如果中間的數(shù)據(jù)改動(dòng)的畫(huà)后面的也相應(yīng)的需要挪動(dòng)。
例如 :一個(gè)a[2][3]的數(shù)組 ,如果需要移除a[1][1] 這個(gè)數(shù)的話 ,后面的處理也要相應(yīng)改動(dòng),有什么方法 能夠更加簡(jiǎn)潔靈活的處理這個(gè)問(wèn)題呢?
?
我的思路就是 將相應(yīng)的處理函數(shù)也放在一個(gè)對(duì)應(yīng)的 b[2][3]數(shù)組里面,這樣不管客戶點(diǎn)擊那個(gè)cell ,我只需要根據(jù)indexpath 調(diào)用相應(yīng)的數(shù)組內(nèi)的處理函數(shù)即可。
?
?
// // ViewController.swift // swift函數(shù)應(yīng)用 // // Created by apple on 15/9/11. // Copyright (c) 2015年 HUNANJIUYIAIJIA. All rights reserved. // import UIKit @IBDesignable class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {@IBOutlet weak var List: UITableView!var ViewControllerArray:[Array< ()->() >]?let Textdescription:[Array<String>]=[["走路","騎車(chē)"],["睡覺(jué)","看書(shū)"]]override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.// self.view.backgroundColor=UIColor.redColor() ViewControllerArray=[[{let Controller=ViewController2(nibName: "ViewController2", bundle: NSBundle.mainBundle())Controller.text="第一個(gè)頁(yè)面";self.navigationController!.pushViewController(Controller, animated: true)},{let Controller=ViewController2(nibName: "ViewController2", bundle: NSBundle.mainBundle())Controller.text="第二個(gè)頁(yè)面";self.navigationController!.pushViewController(Controller, animated: true)} ],[{let Controller=ViewController2(nibName: "ViewController2", bundle: NSBundle.mainBundle())Controller.text="第三個(gè)頁(yè)面";self.navigationController!.pushViewController(Controller, animated: true)},{let Controller=ViewController2(nibName: "ViewController2", bundle: NSBundle.mainBundle())Controller.text="第四個(gè)頁(yè)面";self.navigationController!.pushViewController(Controller, animated: true)} ]];// List.cellLayoutMarginsFollowReadableWidth=true;List.separatorStyle=UITableViewCellSeparatorStyle.SingleLine}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated. }func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {let TabID="cell"var cell:UITableViewCell?=tableView.dequeueReusableCellWithIdentifier(TabID) as UITableViewCell?;if cell==nil {cell=UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: TabID);}cell?.textLabel?.text=Textdescription[indexPath.section][indexPath.row]return cell!;}func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {return 20;}func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return ViewControllerArray!.count}func numberOfSectionsInTableView(tableView: UITableView) -> Int {return ViewControllerArray!.count;}func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {ViewControllerArray![indexPath.section ][indexPath.row]()}}?
轉(zhuǎn)載于:https://www.cnblogs.com/TengSys/p/4835931.html
總結(jié)
- 上一篇: 黑马程序员——数组
- 下一篇: haskell读写文件相关(含二进制)