//swift
// TableViewController.swiftapp
// TableViewasync
//ide
// Created by ITD on 6/8/18.spa
// Copyright © 2018年 ITD. All rights reserved.3d
//orm
import UIKitget
class TableViewController: ViewController,UITableViewDataSource,UITableViewDelegate {it
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {io
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.
}
*/
}