swift tableview 加载更多

//

//  TableViewController.swift

//  TableView

//

//  Created by ITD on 6/8/18.

//  Copyright © 2018年 ITD. All rights reserved.

//

 

import UIKit

 

class TableViewController: ViewController,UITableViewDataSource,UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        

        return marry.count

    }

    //设置section的数量

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1

    }

 

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        //如果是最后一行了

        if indexPath.row == marry.count-1{

            print("滑动到最后了")

            self.loadMoreData()

        }

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        

      

        var cell=table.dequeueReusableCell(withIdentifier: "cellID")

 

        if cell != nil {

            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellID")

        }

        let cellMdic:NSDictionary=(marry[indexPath.row] as? NSDictionary)!

    

        cell?.textLabel?.text=cellMdic.value(forKey: "title") as? String

        cell?.detailTextLabel?.text=cellMdic.value(forKey: "subtitle") as? String

        

        print(cellMdic.value(forKey: "title") as! String,cellMdic.value(forKey: "subtitle") as! String)

       

        return cell!

    }

    

 

var marry = NSMutableArray.init()

 var table:UITableView!

 

    override func viewDidLoad() {

        super.viewDidLoad()

        

        //添加顶部导航

        

        let navBar=UINavigationBar.init(frame: CGRect.init(x: 0, y: 20, width: self.view.frame.width, height: 60))

        let rightButton=UIBarButtonItem.init(barButtonSystemItem: .cancel, target: self, action: #selector(self.close))

       let navItem=UINavigationItem.init(title: "table")

        navItem.setRightBarButton(rightButton, animated: false)

        navBar.pushItem(navItem, animated: true)

        self.view.addSubview(navBar)

        

        //addData

        let addBtn=UIButton.init(frame: .init(x: 0, y: 100, width: 100, height: 60))

        addBtn.setTitle("添加", for: .normal)

        addBtn.addTarget(self, action: #selector(self.addData), for: .touchDown)

        

        addBtn.backgroundColor=UIColor.lightGray

        addBtn.titleColor(for:.application)

        self.view.addSubview(addBtn)

        

        

        self.view.backgroundColor=UIColor.white

        let rect = CGRect.init(x: 0, y: self.view.frame.height*0.55, width: self.view.frame.width, height: self.view.frame.height*0.45)

        table = UITableView(frame: rect)

        self.table.backgroundColor = UIColor.clear

        self.table.dataSource=self

        self.table.delegate=self

    

        self.view.addSubview(table)

        //注册UITableView,cellID为重复使用cell的Identifier

        self.table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")

 

        // Do any additional setup after loading the view.

    }

    

    func loadMoreData()  {

        for i in marry.count...marry.count+10

        {

            let mdic=NSMutableDictionary.init()

            mdic.setValue("title\(i)", forKey: "title")

            mdic.setValue("subtitle\(i)", forKey: "subtitle")

            self.marry.add(mdic)

        }

        DispatchQueue.main.async {

            self.table.reloadData()

        }

    }

    

    @objc func addData()

    {

       

        for i in 0...10

        {

            let mdic=NSMutableDictionary.init()

            mdic.setValue("title\(i)", forKey: "title")

            mdic.setValue("subtitle\(i)", forKey: "subtitle")

            self.marry.add(mdic)

        }

        DispatchQueue.main.async {

             self.table.reloadData()

        }

      

        print(self.marry)

    }

    

    @objc func close()

    {

        self.dismiss(animated: true, completion: nil)

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

 

    /*

    // MARK: - Navigation

 

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */

 

}

posted @ 2018-08-10 15:08  风轻2022  阅读(834)  评论(0编辑  收藏  举报