欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Swift初探

发布时间:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Swift初探 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

 

问题描述:

如上图这是一个根导航器 ,里面包含多个section,和row ,根据用户点击的cell ,进入相应的页面,由于每个页面的初始化都不一样 ,所以大部分的处理都会写成这样:

swtich(index.section) {

   swtich:(index.row) {

//做相应处理

}

}

如果section 和row 比较多 也起来会很烦,而且如果中间的数据改动的画后面的也相应的需要挪动。

例如 :一个a[2][3]的数组 ,如果需要移除a[1][1] 这个数的话 ,后面的处理也要相应改动,有什么方法 能够更加简洁灵活的处理这个问题呢?

 

我的思路就是 将相应的处理函数也放在一个对应的 b[2][3]数组里面,这样不管客户点击那个cell ,我只需要根据indexpath 调用相应的数组内的处理函数即可。

 

 

// // ViewController.swift // swift函数应用 // // 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>]=[["走路","骑车"],["睡觉","看书"]]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="第一个页面";self.navigationController!.pushViewController(Controller, animated: true)},{let Controller=ViewController2(nibName: "ViewController2", bundle: NSBundle.mainBundle())Controller.text="第二个页面";self.navigationController!.pushViewController(Controller, animated: true)} ],[{let Controller=ViewController2(nibName: "ViewController2", bundle: NSBundle.mainBundle())Controller.text="第三个页面";self.navigationController!.pushViewController(Controller, animated: true)},{let Controller=ViewController2(nibName: "ViewController2", bundle: NSBundle.mainBundle())Controller.text="第四个页面";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]()}}

 

转载于:https://www.cnblogs.com/TengSys/p/4835931.html

总结

以上是生活随笔为你收集整理的Swift初探的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。