//
//ViewController.swift
//TableViewDemo
//
//Created by xf-bao on 16/7/20.
//Copyright © 2016年xf-bao. All rights reserved.
//
importUIKit
classViewController:UIViewController{
private var tableView:UITableView!
private var dataSource = [UInt32]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: view.bounds, style: .Plain)
view.addSubview(tableView)
tableView.tableFooterView = UIView()
tableView.dataSource =self
tableView.delegate =self
tableView.alwaysBounceVertical =true
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"RefershCell") // 下拉加载显示的cell
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"NormalCell") // 注册正文cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
return dataSource.count+1 // 正文数据视图 + 刷新视图
}
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{
var cell:UITableViewCell!
if indexPath.row == dataSource.count {
cell = tableView.dequeueReusableCellWithIdentifier("RefershCell")
var v:UIActivityIndicatorView? = cell.contentView.subviews.last as?UIActivityIndicatorView
if let_= v {
}else{
v = UIActivityIndicatorView()
cell.contentView.addSubview(v!)
v?.center = CGPoint(x: CGRectGetWidth(view.bounds)/2, y:40)
v?.activityIndicatorViewStyle = .Gray
v?.color = UIColor.purpleColor()
}
v?.startAnimating()
}else{
cell = tableView.dequeueReusableCellWithIdentifier("NormalCell")
var v:UILabel? = cell.contentView.subviews.last as? UILabel
if let _ = v {
}else{
v = UILabel(frame: CGRect(x:0, y:0, width: CGRectGetWidth(view.bounds), height:40))
cell.contentView.addSubview(v!)
v?.textAlignment = .Center
v?.font = UIFont.boldSystemFontOfSize(22)
}
v?.text = String(dataSource[indexPath.row])
let r = CGFloat(arc4random()%256)/255
let g = CGFloat(arc4random()%256)/255
let b = CGFloat(arc4random()%256)/255
let a = CGFloat(arc4random()%100+1)/100
v?.textColor = UIColor(red: r, green: g, blue: b, alpha: a)
v?.backgroundColor = UIColor(red:1-r, green:1-g, blue:1-b, alpha:1-a)
}
cell.backgroundColor = UIColor.clearColor()
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = UIColor.clearColor()
return cell
}
// 下拉事件即将显示 下拉事件处理 等下拉操作
func tableView(tableView:UITableView, willDisplayCell cell:UITableViewCell, forRowAtIndexPath indexPath:NSIndexPath) {
if indexPath.row == dataSource.count {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(3*NSEC_PER_SEC)), dispatch_get_main_queue(), {
[weak cell, weakself]in
let n = arc4random()%11+10
for_in0..<self?.dataSource.append(arc4random())
}
let v = cell?.contentView.subviews.last as? UIActivityIndicatorView
if let v = v {
v.stopAnimating()
}
self?.tableView.reloadData()
})
}
}
func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) ->CGFloat{
if indexPath.row == dataSource.count {
return 80
}else{
return 40
}
}
}
网友评论