委托2和索引

如下图所示,写一个导航视图控制器,再写一个视图,视图里有文本框,和一个按钮。目的文本框里的值传会导航视图控制器中进行修改更新  

 把Storyboard ID 改为 2

写一个person类来存数据

import UIKit

class Person{
    var name:String
    init(name:String) {
    
    self.name = name
    
    }
    static func getData()->[Person]{
    
    return [
        
        Person(name:"1111"),
        Person(name:"222"),
        Person(name:"333"),
        Person(name:"444"),
        Person(name:"555")
        
        
        ]
    
    
    }

}

导航视图控制器

import UIKit

class testViewController: UITableViewController {
  let person = Person.getData()
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  person.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("demo")
        let p = person[indexPath.row]
        cell?.textLabel?.text = p.name
        return cell!
    }
//    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//        let target = segue.destinationViewController
//        as! TargetViewcontroller
//        let indexPath = tableView.indexPathForSelectedRow
//        //print(indexPath?.row)
//        //print(person[(indexPath?.row)!])
//        target.person = person[(indexPath?.row)!]
//        target.mydelegate = self
//        print("perpare")
//        
//    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let target = storyboard?.instantiateViewControllerWithIdentifier("2")
            as! TargetViewcontroller
        
        let indexPath = tableView.indexPathForSelectedRow
        let p = person[(indexPath?.row)!]
        target.person = p
        target.mydelegate = self
//执行跳转 presentViewController(target, animated:
true, completion: nil) } } extension testViewController : TestDelegate{ func xxx() { //执行更新操作
tableView.reloadData() } }

视图的控制器代码如下

import UIKit

class TargetViewcontroller: UIViewController {
    @IBOutlet weak var txt: UITextField!
    var mydelegate:TestDelegate?
    var person:Person?
    override func viewDidLoad(){
      
        txt.text = person!.name
    }
    
    @IBAction func tojump(sender: UIButton) {
        person?.name = txt.text!
      
        mydelegate?.xxx()
         dismissViewControllerAnimated(true, completion: nil)
    }
    
}

写一个委托

import UIKit

protocol TestDelegate {
     func xxx()
}

索引:直接创建一个导航视图控制器 navigation Controller ;person类和上面的一样

import UIKit


/*
实现索引的步骤:

1.创建节与行
numberOfSectionsInTableView
titleForHeaderInSection

numberOfRowsInSection
cellForRowAtIndexPath

2.添加节索引
sectionIndexTitlesForTableView

3(可选) 处理点击节索引时到底显示哪一个节
前面两步的做法,一般是定义一个键为string,值为数组的数据类型
并把键作为节的所有标题(titles),这样点击某一个title时会自动跳到对应的
节。
如果想让节的标题任意定制,不是字典集合的键,那么就会点击了某个节标题后不知道跳到哪一个节的问题,此时就需要重写
sectionForSectionIndexTitle方法


*/

class indexviewControll: UITableViewController{
    var persons: Dictionary<String, [Person]>!
    var keys:[String]!
    override func viewDidLoad() {
        super.viewDidLoad()
        persons = [
            "a":[Person(name: ""),Person(name: "啊哦")],
            "b":[Person(name: "辟邪"),Person(name: "必须")],
            "c":[Person(name: "草草"),Person(name: "曹仁")],
            "d":[Person(name: "大地"),Person(name: "得到")],
            "e":[Person(name: "a1"),Person(name: "a2")],
            "f":[Person(name: "a1"),Person(name: "a2")],
            "g":[Person(name: "a1"),Person(name: "a2")],
            "h":[Person(name: "a1"),Person(name: "a2")],
            "i":[Person(name: "a1"),Person(name: "a2")],
            "j":[Person(name: "a1"),Person(name: "a2")],
            "k":[Person(name: "a1"),Person(name: "a2")],
            "l":[Person(name: "a1"),Person(name: "a2")],
            
            
        ]
        //设置索引的顺序
        keys = Array(persons.keys).sort(){$0 < $1}  //["a","b"]
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return keys.count
    }
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return keys[section]
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        let personsData = persons[keys[section]]
        
        return personsData!.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        
        let secNum = indexPath.section //得到keys的下标
        let rowNum = indexPath.row  //得到当前行
        let key = keys[secNum] //得到key的值
        let personsData = persons[key] //得到是键的所有person
        let p = personsData![rowNum] //得到对象
        
        cell.textLabel?.text = p.name
        return cell
    }
    //返回右边的keys(索引)
    override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        return keys
    }





}

 

posted @ 2016-01-07 20:56  爱学习~天天学习  阅读(190)  评论(0编辑  收藏  举报