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

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【蜗牛计划,每天进步一点点】 我是清泉 打卡日期:2017年7月4日 打卡天数:第4天 (1)我今年的三个年度目标...
    沈曼柔阅读 897评论 0 2
  • 出差回来路上就和孩子玩起古诗,《渔歌子》之前她没背过,我说我也不会,我读几遍再背给她听,她帮忙检查,我读的时候孩子...
    Yolanda_Hu阅读 1,527评论 0 0
  • 小学同学陈在朋友圈发了一段抱怨的话:明明出国培训的应该是她,怎们又落到别人头上肯定是人走了后门……可她又曾想过,她...
    陪你读书_阅读 2,924评论 0 5
  • 逃离是一种瘾,当你一直暗示自己抗拒现实的时候,一次一次地靠旅行来逃离只会让你越来越厌恶现实。 所以,对于想要逃离现...
    爱你的Lisa阅读 1,261评论 0 0