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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 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 186 187 188 189 190 191 192 193
| import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {
var tableView:UITableView?
var allNames:Dictionary<Int, [String]>?
var adHeaders:[String]?
override func loadView() { super.loadView() }
override func viewDidLoad() { super.viewDidLoad()
self.allNames = [ 0:[String]([ "UILabel 标签", "UIButton 按钮"]), 1:[String]([ "UIDatePiker 日期选择器", "UITableView 表格视图"]) ];
print(self.allNames ?? "")
self.adHeaders = [ "常见 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!) let headerLabel = UILabel(frame: CGRect(x:0, y:0, width:self.view.bounds.size.width, height:30)) headerLabel.backgroundColor = UIColor.black headerLabel.textColor = UIColor.white headerLabel.numberOfLines = 0 headerLabel.lineBreakMode = NSLineBreakMode.byWordWrapping headerLabel.text = "UIKit 控件" headerLabel.font = UIFont.italicSystemFont(ofSize: 20) self.tableView!.tableHeaderView = headerLabel let longPress = UILongPressGestureRecognizer(target:self, action:#selector(ViewController.tableviewCellLongPressed(_:))) longPress.delegate = self longPress.minimumPressDuration = 1.0 self.tableView!.addGestureRecognizer(longPress) }
@objc func tableviewCellLongPressed(_ gestureRecognizer:UILongPressGestureRecognizer) { if (gestureRecognizer.state == UIGestureRecognizerState.began) { print("UIGestureRecognizerStateBegan"); } if (gestureRecognizer.state == UIGestureRecognizerState.changed) { print("UIGestureRecognizerStateChanged"); } if (gestureRecognizer.state == UIGestureRecognizerState.ended) { print("UIGestureRecognizerStateEnded"); if(self.tableView!.isEditing == false) { self.tableView!.setEditing(true, animated:true) } else { self.tableView!.setEditing(false, animated:true) } } }
func numberOfSections(in tableView: UITableView) -> Int { return 2 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let data = self.allNames?[section] return data!.count }
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { var headers = self.adHeaders! return headers[section] }
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { let data = self.allNames?[section] return "有\(data!.count)个控件" }
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 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.tableView!.deselectRow(at: indexPath, animated: true)
let itemString = self.allNames![indexPath.section]![indexPath.row]
let alertController = UIAlertController(title: "提示!", message: "你选中了【\(itemString)】", preferredStyle: .alert) let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil) alertController.addAction(cancelAction) self.present(alertController, animated: true, completion: nil) }
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { if (indexPath.section == 1) { return UITableViewCellEditingStyle.insert } return UITableViewCellEditingStyle.delete }
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? { var data = self.allNames?[indexPath.section]!
let itemString = data![indexPath.row] as String return "确定删除\(itemString)?" }
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if (editingStyle == UITableViewCellEditingStyle.delete) { self.allNames?[indexPath.section]?.remove(at: indexPath.row) self.tableView!.reloadData() self.tableView!.setEditing(true, animated: true) print("你确认了删除按钮") } else if (editingStyle == UITableViewCellEditingStyle.insert) { self.allNames?[indexPath.section]?.insert("插入一项新的", at: indexPath.row+1) print("你按下了插入按钮") self.tableView!.reloadData() } }
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
|