
2,效果图 3,代码如下
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 @H_424_301@ 135 136 137 138 139 140 141 142 143 144 145 146 147 148 @H_404_329@ 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 @H_307_403@ 186 187 188 189 190 191 192 | @H_523_419@ import UIKit class VIEwController : UIVIEwController , UItableVIEwDelegate UItableVIEwDataSource UIGestureRecognizerDelegate { var tableVIEw: UItableVIEw ? allnames: Dictionary < Int String ]>? adheaders:[ ]? overrIDe func loadVIEw() { super .loadVIEw() } vIEwDIDLoad() { .vIEwDIDLoad() //初始化数据,这一次数据,我们放在属性列表文件里 self .allnames = [ 0:[ ]([ "UILabel 标签" String ]([ "UIDatePiker 日期选择器" print ( .allnames) .adheaders = [ "常见 UIKit 控件" ] //创建表视图 .tableVIEw = (frame: .vIEw.frame,style: UItableVIEwStyle . Grouped ) .tableVIEw!.delegate = self .tableVIEw!.dataSource = self //创建一个重用的单元格 .tableVIEw!.registerClass( UItableVIEwCell . "SwiftCell" ) .vIEw.addSubvIEw( .tableVIEw!) //创建表头标签 let headerLabel = UILabel (frame: CGRectMake (0, .vIEw.bounds.size.wIDth,30)) headerLabel.backgroundcolor = UIcolor .blackcolor() headerLabel.textcolor = UIcolor .whitecolor() headerLabel.numberOflines = 0 headerLabel.lineBreakMode = NSlineBreakMode ByWorDWrapPing headerLabel.text = "UIKit 控件" headerLabel.Font = UIFont .italicSystemFontOfSize(20) .tableVIEw!.tableheaderVIEw = headerLabel //绑定对长按的响应 longPress = UILongPressGestureRecognizer (target: Selector ( "tablevIEwCellLongpressed:" )) //代理 longPress.delegate = self longPress.minimumPressDuration = 1.0 //将长按手势添加到需要实现长按 *** 作的视图里 .tableVIEw!.addGestureRecognizer(longPress) } tablevIEwCellLongpressed(gestureRecognizer: ) { if (gestureRecognizer.state == UIGestureRecognizerState Began ) { "UIGestureRecognizerStateBegan" ); } Changed ) { "UIGestureRecognizerStateChanged" ); } (gestureRecognizer.state == UIGestureRecognizerState Ended ) { "UIGestureRecognizerStateEnded" ); //在正常状态和编辑状态之间切换 .tableVIEw!.editing == false ) @H_403_830@{ .tableVIEw!.setEditing( true ) } else { ) } } } //在本例中,有2个分区 numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw ) -> { return 2 } //返回表格行数(也就是返回控件数) tableVIEw(tableVIEw: { data = .allnames?[section] data!.count } // UItableVIEwDataSource协议中的方法,该方法的返回值决定指定分区的头部 tableVIEw(tableVIEw: )-> ? { headers = .adheaders!; headers[section]; } // UItableVIEwDataSource协议中的方法,该方法的返回值决定指定分区的尾部 ? { .allnames?[section] return "有\(data!.count)个控件" } //创建各单元显示内容(创建参数indexPath指定的单元) NSIndexPath ) -> UItableVIEwCell { //为了提供表格显示性能,已创建完成的单元需重复使用 IDentify: = "SwiftCell" //同一形式的单元格重复使用,在声明时已注册 cell = tableVIEw.dequeueReusableCellWithIDentifIEr(IDentify,forIndexPath: indexPath) as UItableVIEwCell cell.accessoryType = UItableVIEwCellAccessoryType disclosureIndicator secno = indexPath.section .allnames?[secno] cell.textLabel?.text = data![indexPath.row] cell } // UItableVIEwDelegate 方法,处理列表项的选中事件 ) { .tableVIEw!.deselectRowAtIndexPath(indexPath,monospace!important; min-height:inherit!important; background:none!important">) itemString = .allnames![indexPath.section]![indexPath.row] alertvIEw = UIAlertVIEw (); alertvIEw.Title = "提示!" alertvIEw.message = "你选中了【\(itemString)】" ; alertvIEw.addbuttonWithTitle( "确定" ) alertvIEw.show(); } NSIndexPath ) -> UItableVIEwCellEditingStyle { (indexPath.section == 1) { return UItableVIEwCellEditingStyle Insert } return UItableVIEwCellEditingStyle Delete } TitleForDeleteConfirmationbuttonForRowAtIndexPath indexPath: ) -> ? { .allnames?[indexPath.section]! itemString = data![indexPath.row] as String "确定删除\(itemString)?" } ) { (editingStyle == Delete ) { .allnames?[indexPath.section]?.removeAtIndex(indexPath.row) .tableVIEw!.reloadData() ) "你确认了删除按钮" ) // Array } else (editingStyle == Insert ) { .allnames?[indexPath.section]?.insert( "插入一项新的" } } dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() } } |
以上是内存溢出为你收集整理的Swift - 给表格添加编辑功能(删除,插入)全部内容,希望文章能够帮你解决Swift - 给表格添加编辑功能(删除,插入)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)