import UIKit class ViewController : UIViewController , UITableViewDelegate , UITableViewDataSource , UIGestureRecognizerDelegate , SwipeableSectionHeaderDelegate { var tableView: UITableView ! var adHeaders:[ String ]! var allnames:[[ String ]]! override func loadView() { super .loadView() } override func viewDidLoad() { super .viewDidLoad() //初始化数据,这一次数据,我们放在属性列表文件里 self .allnames = [[ "UILabel 标签" , "UIButton 按钮" ], [ "UIDatePiker 日期选择器" , "UITableView 表格视图" ], [ "UICollectionView 网格" ] ] self .adHeaders = [ "常见 UIKit 控件" , "中级 UIKit 控件" , "高级 UIKit 控件" ] //创建表视图 self .tableView = UITableView (frame: self .view.frame, style:.grouped) self .tableView.delegate = self self .tableView.dataSource = self //创建一个重用的单元格 self .tableView.register( UITableViewCell . self , forCellReuseIdentifier: "SwiftCell" ) self .view.addSubview( self .tableView) } //在本例中,有3个分区 func numberOfSections( in tableView: UITableView ) -> Int { return self .adHeaders.count } //返回表格行数(也就是返回控件数) func tableView(_ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int { let data = self .allnames[section] return data.count } //返回自定义的分区头 func tableView(_ tableView: UITableView , viewForHeaderInSection section: Int ) -> UIView ? { let headerView = SwipeableSectionHeader () //设置代理 headerView.delegate = self //设置标题 headerView.titleLabel.text = self .adHeaders[section] //设置分区索引 headerView.section = section //设置手势优先级(否则将与表格自带的手势冲突,造成滑动分区头时出现第一个cell的删除按钮) if let gestureRecognizers = tableView.gestureRecognizers { for recognizer in gestureRecognizers { recognizer.require(toFail: headerView.swipeLeft) } } return headerView } //返回分区头部高度 func tableView(_ tableView: UITableView , heightForHeaderInSection section: Int ) -> CGFloat { return 40 } // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部 func tableView(_ tableView: UITableView , titleForFooterInSection section: Int ) -> String ? { let data = self .allnames[section] return "有\(data.count)个控件" } //创建各单元显示内容(创建参数indexPath指定的单元) func tableView(_ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell { //为了提供表格显示性能,已创建完成的单元需重复使用 let identify: String = "SwiftCell" //同一形式的单元格重复使用,在声明时已注册 let cell = tableView.dequeueReusableCell(withIdentifier: identify, for : indexPath) cell.accessoryType = .disclosureIndicator let secno = indexPath.section var data = self .allnames[secno] cell.textLabel?.text = data[indexPath.row] return cell } //删除整个分区 func deleteSection(section: Int ) { self .adHeaders.remove(at: section) self .allnames.remove(at: section) self .tableView.reloadData() } //设置单元格的编辑的样式 func tableView(_ tableView: UITableView , editingStyleForRowAt indexPath: IndexPath ) -> UITableViewCellEditingStyle { return UITableViewCellEditingStyle .delete } //设置确认删除按钮的文字 func tableView(_ tableView: UITableView , titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath ) -> String ? { return "删除" } //单元格编辑后(删除或插入)的响应方法 func tableView(_ tableView: UITableView , commit editingStyle: UITableViewCellEditingStyle , forRowAt indexPath: IndexPath ) { self .allnames[indexPath.section].remove(at: indexPath.row) self .tableView.reloadData() print ( "你确认了删除按钮" ) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } |