Swift学习之UITableView的实现以及滑动删除
2016-01-13 22:29
363 查看
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!)
}
//返回有多少个分组
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 {
return UITableViewCellEditingStyle.Delete
}
//在这里修改删除按钮的文字
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "点击删除"
}
//点击删除按钮的响应方法,在这里处理删除的逻辑
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
self.dataList!.removeAtIndex(indexPath.row)
self.tableView!.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
}
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!)
}
//返回有多少个分组
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 {
return UITableViewCellEditingStyle.Delete
}
//在这里修改删除按钮的文字
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "点击删除"
}
//点击删除按钮的响应方法,在这里处理删除的逻辑
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
self.dataList!.removeAtIndex(indexPath.row)
self.tableView!.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
}
相关文章推荐
- 对上一篇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枚举详解
- Swift闭包详解