swift初学TableView列表

    技术2022-07-10  134

    废话不多说直接上代码

    下面展示一些 内联代码片。

    先挂上列表的两个代理 class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource 声明两个变量 var ctrlnames:[String]? var tableView:UITableView? 下面代码 override func loadView() { super.loadView() } override func viewDidLoad() { super.viewDidLoad() //初始化数据,这一次数据,我们放在属性列表文件里 self.ctrlnames = ["wew","wrewtr","rewqtewq","geqgewq","ertrewgcds"]; print(self.ctrlnames ?? 32) //创建表视图 self.tableView = UITableView(frame: self.view.frame, style:UITableView.Style.plain) self.tableView!.delegate = self self.tableView!.dataSource = self //创建一个重用的单元格 self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell") self.view.addSubview(self.tableView!) //创建表头标签 let headerLabel = UILabel.init(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: UIScreen.main.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 } //在本例中,只有一个分区 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } //返回表格行数(也就是返回控件数) func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.ctrlnames!.count } //创建各单元显示内容(创建参数indexPath指定的单元) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //为了提供表格显示性能,已创建完成的单元需重复使用 let identify:String = "SwiftCell" //同一形式的单元格重复使用,在声明时已注册 let cell = tableView.dequeueReusableCell(withIdentifier: identify, for: indexPath as IndexPath) as UITableViewCell cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator cell.textLabel?.text = self.ctrlnames![indexPath.row] return cell } //点击cell事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.tableView!.deselectRow(at: indexPath as IndexPath, animated: true) let itemString = self.ctrlnames![indexPath.row] let alertController = UIAlertController(title: "提示!", message: "你选中了【\(itemString)】", preferredStyle: .alert) let okAction = UIAlertAction(title: "确定", style: .default,handler: nil) alertController.addAction(okAction) self.present(alertController, animated: true, completion: nil) } //滑动删除必须实现的方法 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { print("删除\(indexPath.row)") self.ctrlnames?.remove(at: indexPath.row) self.tableView?.deleteRows(at: [indexPath], with: UITableView.RowAnimation.top) } //滑动删除 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { return UITableViewCell.EditingStyle.delete } //修改删除按钮的文字 func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? { return "删" } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
    Processed: 0.011, SQL: 9