ios TableView那些事3(Swift 三) TableView 的删除操作
2014-12-01 00:37
531 查看
删除tableview 通常 手势滑动删除 和导航放个管理删除按钮 下面实现的就是这两种方法
import UIKit
class ViewController: UITableViewController {
var dataScoure = ["one","hello","miss","ios","good"]
var barButtonItem:UIBarButtonItem?;
override func viewDidLoad() {
super.viewDidLoad()
setRightDeleteButtonItem()
// Do any additional setup after loading the view, typically from a nib.
}
//设置导航右边管理按钮
func setRightDeleteButtonItem(){
barButtonItem = UIBarButtonItem(title:"管理", style: .Done, target: self, action: "RightButtonItemAction")
barButtonItem!.tag = 10;
self.navigationItem.rightBarButtonItem = barButtonItem
}
func RightButtonItemAction(){
if barButtonItem?.tag == 10{
self.tableView.setEditing(true, animated: true);
barButtonItem!.title = "删除"
barButtonItem!.tag = 20;
}
else{
self.tableView.setEditing(false, animated: true);
barButtonItem!.title = "管理"
barButtonItem!.tag = 20;
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1;
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return dataScoure.count;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = dataScoure[indexPath.row];
return cell;
}
//删除函数
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
//删除数据源的对应数据
dataScoure.removeAtIndex(indexPath.row)
//删除对应的cell
self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
//数据源为空的时候管理按钮不能删除
if self.dataScoure.count == 0{
barButtonItem?.enabled = false;
}
}
//把delete 该成中文
override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String! {
return "删除"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class ViewController: UITableViewController {
var dataScoure = ["one","hello","miss","ios","good"]
var barButtonItem:UIBarButtonItem?;
override func viewDidLoad() {
super.viewDidLoad()
setRightDeleteButtonItem()
// Do any additional setup after loading the view, typically from a nib.
}
//设置导航右边管理按钮
func setRightDeleteButtonItem(){
barButtonItem = UIBarButtonItem(title:"管理", style: .Done, target: self, action: "RightButtonItemAction")
barButtonItem!.tag = 10;
self.navigationItem.rightBarButtonItem = barButtonItem
}
func RightButtonItemAction(){
if barButtonItem?.tag == 10{
self.tableView.setEditing(true, animated: true);
barButtonItem!.title = "删除"
barButtonItem!.tag = 20;
}
else{
self.tableView.setEditing(false, animated: true);
barButtonItem!.title = "管理"
barButtonItem!.tag = 20;
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1;
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return dataScoure.count;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = dataScoure[indexPath.row];
return cell;
}
//删除函数
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
//删除数据源的对应数据
dataScoure.removeAtIndex(indexPath.row)
//删除对应的cell
self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
//数据源为空的时候管理按钮不能删除
if self.dataScoure.count == 0{
barButtonItem?.enabled = false;
}
}
//把delete 该成中文
override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String! {
return "删除"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
相关文章推荐
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- ios tableView那些事 (九) tableview的删除
- iOS之tableView(五)的编辑删除插入操作和UIAlertController的使用
- ios tableView那些事 (九) tableview的删除
- ios tableView那些事 (九) tableview的删除
- ios tableView那些事 (九) tableview的删除
- ios TableView那些事(三十 四)TableView 单选操作
- ios TableView那些事(三十一)UITableViewController ios8 删除时添加多个按钮
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除
- iOS开发系列 ---- UI (TableView的编辑和多项删除操作)
- ios TableView那些事(三十一)UITableViewController ios8 删除时添加多个按钮
- ios tableView那些事 (九) tableview的删除
- ios tableView那些事 (十一) 让 tableview 不可滚动或屏蔽掉
- ios tableView那些事 (十八) UITableView单行刷新数据
- ios tableView那些事 (十一) 让 tableview 不可滚动或屏蔽掉
- iOS tableView 如何禁止滑动手势删除,只允许在编辑模式下进行删除Cell
- ios tableView那些事 (五) 给tableview设置缩进级别
- ios tableView那些事 (十三) 给 tableview 添加索引
- ios tableView那些事 (十七) UITableView的下拉刷新