美文网首页
表格cell点击跳转的使用

表格cell点击跳转的使用

作者: GraySuit | 来源:发表于2018-10-25 18:21 被阅读0次
屏幕快照 2018-10-25 18.10.10.png

一.首先在APPDelegate中创建导航条

    let vc:ViewController = ViewController()
    let nav = UINavigationController(rootViewController: vc)
    
    self.window?.rootViewController = nav

二.在ViewController中创建表格及跳转

import UIKit
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

var tbv:UITableView?
var nameArr:[String] = []

var dict:NSDictionary = [:]
var data:NSMutableDictionary = [:]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    // 生成文件的存储路径
    let plistPath = Bundle.main.path(forResource: "plist", ofType: "plist")
    
    //读取属性列表文件,并转化为可变字典对象
    data = NSMutableDictionary(contentsOfFile: plistPath!)!

    nameArr = data.allKeys as! [String]
    
    tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    view.addSubview(tbv!)
    
    tbv?.delegate = self
    tbv?.dataSource = self
    
    tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
    
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   // return nameArr.count
    return nameArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
    cell.textLabel?.text = nameArr[indexPath.row];
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if nameArr[indexPath.row]=="国外"{
        dict = data["国外"] as! NSDictionary
        let vc:SecondVC = SecondVC()
        
        vc.dict = dict
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }else{
        
        dict = data["国内"] as! NSDictionary
        
        let vc:ThreeVC = ThreeVC()
        
        vc.dict = dict
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

}

三.在SecondVC中

class SecondVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var dict:NSDictionary = [:]
var tbv:UITableView?

var nameArr:[String] = []


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
    view.backgroundColor = UIColor.white
    
    nameArr = dict.allKeys as! [String];
    
    tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    view.addSubview(tbv!)
    
    tbv?.delegate = self
    tbv?.dataSource = self
    
    tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
    
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
    cell.textLabel?.text = nameArr[indexPath.row];
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if nameArr[indexPath.row]=="Britney Spears"{
        
        let arr:NSArray = dict["Britney Spears"] as! NSArray
        
        let vc:FourVC = FourVC()
        
        vc.dict = arr
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }else{
        let arr:NSArray = dict["Kelly Clarkson"] as! NSArray
        let vc:FourVC = FourVC()
        
        vc.dict = arr
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

}

四.ThreeVC
class ThreeVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var dict:NSDictionary = [:]
var tbv:UITableView?

var nameArr:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
    view.backgroundColor = UIColor.white
    
    nameArr = dict.allKeys as! [String];
    
    tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    view.addSubview(tbv!)
    
    tbv?.delegate = self
    tbv?.dataSource = self
    
    tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
    cell.textLabel?.text = nameArr[indexPath.row];
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if nameArr[indexPath.row]=="大陆"{
        
        let dic:NSDictionary = dict["大陆"] as! NSDictionary
        
        let vc:FiveVC = FiveVC()
        
        vc.dict = dic
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }else{
        let dic:NSDictionary = dict["港台"] as! NSDictionary
        let vc:SixVC = SixVC()
        
        vc.dict = dic
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

}

五.FourVC中

class FourVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var dict:NSArray = []
var tbv:UITableView?

// var nameArr:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
    view.backgroundColor = UIColor.white
    
    tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    view.addSubview(tbv!)
    
    tbv?.delegate = self
    tbv?.dataSource = self
    
    tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dict.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
    cell.textLabel?.text = dict[indexPath.row] as? String;
    return cell
}

}

六.在FiveVC中

class FiveVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var dict:NSDictionary = [:]
var tbv:UITableView?

var nameArr:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    view.backgroundColor = UIColor.white
    
    nameArr = dict.allKeys as! [String];
    
    tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    view.addSubview(tbv!)
    
    tbv?.delegate = self
    tbv?.dataSource = self
    
    tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
    cell.textLabel?.text = nameArr[indexPath.row];
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if nameArr[indexPath.row]=="鹿晗"{
        
        let arr:NSArray = dict["鹿晗"] as! NSArray
        
        let vc:FourVC = FourVC()
        
        vc.dict = arr
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }else{
        let arr:NSArray = dict["王凯俊"] as! NSArray
        let vc:FourVC = FourVC()
        
        vc.dict = arr
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

}

七.在SixVC中

class SixVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var dict:NSDictionary = [:]
var tbv:UITableView?

var nameArr:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
    view.backgroundColor = UIColor.white
    
    nameArr = dict.allKeys as! [String];
    
    tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    view.addSubview(tbv!)
    
    tbv?.delegate = self
    tbv?.dataSource = self
    
    tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
    
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
    cell.textLabel?.text = nameArr[indexPath.row];
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if nameArr[indexPath.row]=="周杰伦"{
        
        let arr:NSArray = dict["周杰伦"] as! NSArray
        
        let vc:FourVC = FourVC()
        
        vc.dict = arr
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }else if nameArr[indexPath.row]=="刘德华"{
        let arr:NSArray = dict["刘德华"] as! NSArray
        let vc:FourVC = FourVC()
        
        vc.dict = arr
        
        self.navigationController?.pushViewController(vc, animated: true)
    }else{
        
        let arr:NSArray = dict["谢霆锋"] as! NSArray
        let vc:FourVC = FourVC()
        vc.dict = arr
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

}

八.Plist文件中


屏幕快照 2018-10-25 18.20.10.png

相关文章

网友评论

      本文标题:表格cell点击跳转的使用

      本文链接:https://www.haomeiwen.com/subject/pzmctqtx.html