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)
}
}
}