Swift学习之UITableView 排序
2016-01-13 22:48
369 查看
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var dataList:[String]?
var tableView:UITableView?
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.dataList! = ["数据1", "数据2", "数据3", "数据4", "数据5", "数据6", "数据7", "数据8", "数据9", "数据10", "数据11", "数据12", "数据13", "数据14"];
self.tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
self.tableView!.dataSource = self;
self.tableView!.delegate = self;
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.view.addSubview(tableView!)
let editButton:UIButton = UIButton(frame: CGRectMake(0, 0, 100, 50))
editButton.backgroundColor = UIColor.blueColor()
editButton.setTitle("编辑", forState: UIControlState.Normal)
editButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
editButton.addTarget(self, action: Selector("clickButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(editButton)
}
func clickButtonAction(button:UIButton) {
if self.tableView!.editing {
self.tableView!.editing = false
button.setTitle("编辑", forState: UIControlState.Normal)
} else {
self.tableView!.editing = true
button.setTitle("完成", forState: UIControlState.Normal)
}
}
//返回有多少个分组
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//每个分组对应的cell个数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataList!.count
}
//创建Cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier:String = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = self.dataList![indexPath.row]
return cell;
}
//处理选中事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
print("选中的Cell 为\(self.dataList![indexPath.row])")
}
//返回编辑类型
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
//设置为None就没有滑动删除
return UITableViewCellEditingStyle.None
}
//是否允许移动
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
//移动后的事件处理
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
}
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var dataList:[String]?
var tableView:UITableView?
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.dataList! = ["数据1", "数据2", "数据3", "数据4", "数据5", "数据6", "数据7", "数据8", "数据9", "数据10", "数据11", "数据12", "数据13", "数据14"];
self.tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
self.tableView!.dataSource = self;
self.tableView!.delegate = self;
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.view.addSubview(tableView!)
let editButton:UIButton = UIButton(frame: CGRectMake(0, 0, 100, 50))
editButton.backgroundColor = UIColor.blueColor()
editButton.setTitle("编辑", forState: UIControlState.Normal)
editButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
editButton.addTarget(self, action: Selector("clickButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(editButton)
}
func clickButtonAction(button:UIButton) {
if self.tableView!.editing {
self.tableView!.editing = false
button.setTitle("编辑", forState: UIControlState.Normal)
} else {
self.tableView!.editing = true
button.setTitle("完成", forState: UIControlState.Normal)
}
}
//返回有多少个分组
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//每个分组对应的cell个数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataList!.count
}
//创建Cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier:String = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = self.dataList![indexPath.row]
return cell;
}
//处理选中事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
print("选中的Cell 为\(self.dataList![indexPath.row])")
}
//返回编辑类型
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
//设置为None就没有滑动删除
return UITableViewCellEditingStyle.None
}
//是否允许移动
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
//移动后的事件处理
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
}
}
相关文章推荐
- Swift学习之UITableView的实现以及滑动删除
- 对上一篇Swift中的as? 与 as!的补充
- 窥探 Swift 之 函数与闭包的应用实例
- 关于swift中出现 use of undeclared type ***错误的一种解决方法
- swift NSArray(数组)
- iOS开发swift自定义AlertController弹出带有输入框后AertView
- iOS- Swift实现UITableView的常见操
- swift 用协议实现代理传值功能
- swift举例
- Swift基础--使用TableViewController自定义列表
- 菜鸟探究Swift中!与?的神秘
- Swift 的结构体
- 在OC和Swift中使用IBDesignable/IBInspectable
- Swift附属脚本
- Swift方法
- swift 如何实现点击view后显示灰色背景
- Swift属性
- Swift类和结构体
- Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)
- Swift枚举详解