zoukankan      html  css  js  c++  java
  • 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]()
        }
     
    }
  • 相关阅读:
    poj 2229 Sumsets
    HDU- 2063 过山车
    编写一个简易购物车,实现向购物车内添加商品,移除指定商品及清空购物车功能。
    编写一个实现页面计数,要求当刷新页面时,不增加计数
    4-1:编写一个简单的留言簿,写入留言提交后显示留言内容。
    HDU-3706 Second My Problem First
    HDU-1896 Stones
    4-16 表单提交信息和获取。
    HDU-1873 看病要排队
    HDU-1509 Windows Message Queue
  • 原文地址:https://www.cnblogs.com/TengSys/p/4835931.html
Copyright © 2011-2022 走看看