显示简单的UITableView
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var tableView: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: view.bounds, style: .Plain)
if let theTableView = tableView{
theTableView.registerClass(UITableViewCell.classForCoder(),
forCellReuseIdentifier: "identifier")
theTableView.dataSource = self
theTableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
view.addSubview(theTableView)
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
switch section{
case 0:
return 3
case 1:
return 5
case 2:
return 8
default:
return 0
}
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("identifier",
forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = "Section \(indexPath.section), " +
"Cell \(indexPath.row)"
return cell
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
效果如下:
滑动删除
import UIKit
class ViewController: UIViewController,
UITableViewDataSource, UITableViewDelegate {
var tableView: UITableView?
var allRows = [String]()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
for index in 0..<10{
allRows.append("Cell at index of \(index)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.setLeftBarButtonItem(editButtonItem(), animated: false)
tableView = UITableView(frame: view.bounds, style: .Plain)
if let theTableView = tableView{
theTableView.registerClass(UITableViewCell.classForCoder(),
forCellReuseIdentifier: "identifier")
theTableView.dataSource = self
theTableView.delegate = self
theTableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
view.addSubview(theTableView)
}
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return allRows.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("identifier",
forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = allRows[indexPath.row]
return cell
}
func tableView(tableView: UITableView,
editingStyleForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCellEditingStyle{
return .Delete
}
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView!.setEditing(editing, animated: animated)
}
func tableView(tableView: UITableView,
commitEditingStyle editingStyle: UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == .Delete{
/* First remove this object from the source */
allRows.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
}
}
}
效果如下:
设置Header和Footer
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String?{
return "Section \(section) Header"
}
func tableView(tableView: UITableView,
titleForFooterInSection section: Int) -> String?{
return "Section \(section) Footer"
}
效果如下:
下拉刷新
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var tableView: UITableView?
var allTimes = [NSDate]()
var refreshControl: UIRefreshControl?
override func viewDidLoad() {
super.viewDidLoad()
allTimes.append(NSDate())
tableView = UITableView(frame: view.bounds, style: .Plain)
if let theTableView = tableView{
theTableView.registerClass(UITableViewCell.classForCoder(),
forCellReuseIdentifier: "identifier")
theTableView.dataSource = self
theTableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
/* Create the refresh control */
refreshControl = UIRefreshControl()
refreshControl!.addTarget(self,
action: "handleRefresh:",
forControlEvents: .ValueChanged)
theTableView.addSubview(refreshControl!)
view.addSubview(theTableView)
}
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return allTimes.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("identifier",
forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = "\(allTimes[indexPath.row])"
return cell
}
func handleRefresh(paramSender: AnyObject){
/* Put a bit of delay between when the refresh control is released
and when we actually do the refreshing to make the UI look a bit
smoother than just doing the update without the animation */
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC))
dispatch_after(popTime,
dispatch_get_main_queue(), {
/* Add the current date to the list of dates that we have
so that when the table view is refreshed, a new item will appear
on the screen so that the user will see the difference between
the before and the after of the refresh */
self.allTimes.append(NSDate())
self.refreshControl!.endRefreshing()
let indexPathOfNewRow = NSIndexPath(forRow: self.allTimes.count - 1,
inSection: 0)
self.tableView!.insertRowsAtIndexPaths([indexPathOfNewRow],
withRowAnimation: .Automatic)
})
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
效果如下: