标签:
表格(tableview)的确是一个很好用的控件,现在来实现一个编辑功能的实现,包含添加和删除,删除包括长按删除和左滑删除
效果图如下:
具体代码如下:
1、创建表格(这个表格有2个区,有区头和区尾),以及长按手势的方法绑定
class TenthViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate { var tableView:UITableView? var oneArray = NSMutableArray() var twoArray = NSMutableArray() var array = NSMutableArray() var hadHeaders:[String]? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.view.backgroundColor = UIColor.whiteColor() self.title = "表格的编辑功能(删除、插入)" initTabelView() oneArray = ["1","2","3"]; twoArray = ["one","two","three"]; array.addObjectsFromArray([oneArray,twoArray]) } func initTabelView() { let headerLab = UILabel() headerLab.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20) headerLab.backgroundColor = UIColor.orangeColor() headerLab.textColor = UIColor.whiteColor() headerLab.numberOfLines = 0 headerLab.lineBreakMode = NSLineBreakMode.ByWordWrapping headerLab.text = "这个是个表头" headerLab.font = UIFont.systemFontOfSize(20) let footerLab = UILabel() footerLab.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20) footerLab.backgroundColor = UIColor.orangeColor() footerLab.textColor = UIColor.whiteColor() footerLab.numberOfLines = 0 footerLab.lineBreakMode = NSLineBreakMode.ByWordWrapping footerLab.text = "这个是个表尾" footerLab.font = UIFont.systemFontOfSize(20) self.hadHeaders = [ "这是第一个区", "这是第二个区" ] tableView = UITableView(frame: self.view.frame, style: .Plain) tableView!.rowHeight = 50 tableView!.delegate = self tableView!.dataSource = self tableView?.tableFooterView = footerLab tableView?.tableHeaderView = headerLab self.view.addSubview(tableView!) //注册一个重用的单元格 tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "swiftcells") //绑定对长按的响应 let longPress = UILongPressGestureRecognizer(target: self,action: #selector(tableviewCellLongPressed(_:))) //代理 longPress.delegate = self longPress.minimumPressDuration = 1.0 //将长按手势添加到需要实现长按操作的视图里 self.tableView!.addGestureRecognizer(longPress) }
2、长按删除的响应方法的实现
func tableviewCellLongPressed(gestureRecognizer:UILongPressGestureRecognizer) { if gestureRecognizer.state == UIGestureRecognizerState.Began { print("开始") } if gestureRecognizer.state == UIGestureRecognizerState.Changed { print("已经选择") } if gestureRecognizer.state == UIGestureRecognizerState.Ended { print("结束") //在正常状态和编辑状态之间切换 if tableView?.editing == false { tableView?.setEditing(true, animated: true) }else{ tableView?.setEditing(false, animated: true) } } }
3、表格具体的代理方法的实现
func numberOfSectionsInTableView(tableView: UITableView) -> Int { return array.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.objectAtIndex(section).count } //设置区头 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { var headers = self.hadHeaders! return headers[section] } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let identify:String = "swiftcells" let cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath) as UITableViewCell cell.selectionStyle = UITableViewCellSelectionStyle.None cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator cell.textLabel?.text = array.objectAtIndex(indexPath.section).objectAtIndex(indexPath.row) as? String return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) let itemString = array[indexPath.section][indexPath.row] let alertView = UIAlertView() alertView.title = "提示" alertView.message = "你选中了\(itemString)" alertView.addButtonWithTitle("确定") alertView.show() } func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { //第一个区是插入操作,第二个去是删除操作 if indexPath.section == 1 { return UITableViewCellEditingStyle.Insert //插入 } return UITableViewCellEditingStyle.Delete //删除 } //左滑删除的具体实现 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == UITableViewCellEditingStyle.Delete { array[indexPath.section].removeObjectAtIndex(indexPath.row) tableView.reloadData() tableView.setEditing(true, animated: true) }else if editingStyle == UITableViewCellEditingStyle.Insert { array[indexPath.section].insertObject("插入一项新的", atIndex: indexPath.row + 1) tableView.reloadData() } }
这样就实现了上图的效果!
标签:
原文地址:http://www.cnblogs.com/hero11223/p/5736509.html