美文网首页iOS程序猿
IOS:Swift-cell的重用机制02与传值

IOS:Swift-cell的重用机制02与传值

作者: 任任任任师艳 | 来源:发表于2016-11-29 17:52 被阅读0次

AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
    self.window?.makeKeyAndVisible()
   
    let vc = ViewController()
    let navigation = UINavigationController(rootViewController: vc)
    self.window?.rootViewController = navigation
    
    
    return true
}

ViewController.swift:
/*
cell重用的第二种方式
1使用TableView注册cell,注册的同时给cell重用标识
2.tableView根据重用标识完成重用
*/
//屏幕的宽
let kScreenWidth = UIScreen.main.bounds.size.width
//屏幕的高
let kScreenHeigh = UIScreen.main.bounds.size.height

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var tableView:UITableView!
    //定义重用标识
    let identifier = "cell"
    override func loadView() {
        tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
        //数据源代理
        tableView.dataSource = self
        //业务代理属性
        tableView.delegate = self
        
        
        self.view = tableView
    
    }
 override func viewDidLoad() {
        super.viewDidLoad()
              //注册cell
        //UITableViewCell.self h获取一个类的对象
        //UITableViewCell.classForCoder()  获取一个类的对象
        //参数一:类对象
        //参数二:这种cell的重用标识
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
    
    }

//返回分区中cell个数的方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    //返回展示数据的cell
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        cell?.textLabel?.text = "测试"
        cell?.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
        return cell!
    }

 //实现分区的方法
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 3
    }
    //MARK:-UITableViewDelegate的相关方法
    //设置偶数40  奇数80的高  indexpath
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        //给不同的cell设置高度  rowheigh适用于所有
       return (indexPath.section % 2) == 0 ? 40 : 80
    }
    //返回分区的区头的高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 100
    }
    //返回分区尾部的高度
    func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
        if section == 0{
        return 50
        }
        else{
        return 100
        }
    }

  //返回分区 区头的方法
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 100))
        sectionHeaderView.backgroundColor = UIColor.yellow
        return sectionHeaderView
    }
    //返回分区 区尾的方法
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let sectionfooterView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 50))
        sectionfooterView.backgroundColor = UIColor.blue
        return sectionfooterView
        
    }
    //************重点中的重点
    //选中cell触发的方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //创建要跳转的界面   第二步
        let detileVC = DetailViewController()
    detileVC.indexpath = indexPath
        self.navigationController?.pushViewController(detileVC, animated: true)
        print("分区\(indexPath.section),行号\(indexPath.row)")
        
    }

DetailViewController.swift:

 var label :UILabel!
    //定义要传值的类型的属性  属性传值第一步
    var indexpath:IndexPath!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        label = UILabel(frame: CGRect(x: 100, y: 150, width: kScreenWidth - 200, height: 40))
        label.backgroundColor = UIColor.purple
        self.view.addSubview(label)
        //第三步使用属性的值
        label.text = "分区\(indexpath.section),行号\(indexpath.row)"
        self.view.backgroundColor = UIColor.cyan

相关文章

网友评论

    本文标题:IOS:Swift-cell的重用机制02与传值

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