• Swift


    import UIKit
    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
        
        var ctrlnames:[String]?
        var tableView:UITableView?
      
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.ctrlnames = ["UILabel","UITextField","UIButton","UISwitch","UISegmentControl","UIImageView","UIProgressView","UISlider","UIAlertView"]
            
            self.tableView = UITableView(frame:self.view.frame, style: .plain)
            self.tableView?.delegate = self
            self.tableView?.dataSource = self
            
            self.tableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
            self.view.addSubview(self.tableView!)
            
            //创建表头标签
            let frame = CGRect(x: 0, y: 0,  self.view.bounds.size.width, height: 30)
            let headerLabel = UILabel(frame: frame)
            headerLabel.backgroundColor = UIColor.black
            headerLabel.textColor = UIColor.white
            headerLabel.numberOfLines = 0
            headerLabel.lineBreakMode = .byWordWrapping
            headerLabel.text = "常见的 UIKit 控件"
            headerLabel.font = UIFont.italicSystemFont(ofSize: 20)
            self.tableView?.tableHeaderView = headerLabel
        }
        
        //表格行数
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.ctrlnames!.count
        }
        
        //单元显示内容
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let identyfy:String = "cellID"
            let cell = tableView.dequeueReusableCell(withIdentifier: identyfy, for: indexPath)
            cell.accessoryType = .disclosureIndicator
            cell.textLabel?.text = self.ctrlnames![indexPath.row]
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            self.tableView?.deselectRow(at: 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: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
            let index = indexPath.row
            self.ctrlnames?.remove(at: index)
            self.tableView?.deleteRows(at: [indexPath], with: .bottom)
        }
        
        //滑动删除
        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
            return .delete
        }
        //修改删除按钮的文字
        func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
            return "删除"
        }
        
    }
    
  • 相关阅读:
    windows8开发学习笔记
    文字超出隐藏并显示省略号
    list-style-type -- 定义列表样式
    抄录:系统集成项目管理42个子过程
    系统集成项目管理工程师--总结(4章)
    系统集成管理项目工程师总结--口诀2(包含1)
    系统集成管理项目工程师总结--法律法规日期篇1
    关于EAI(一个同学问了我很可爱的问题所以查到的这些)要看看
    系统集成项目管理工程师计划
    中级
  • 原文地址:https://www.cnblogs.com/baidaye/p/9323724.html
Copyright © 2020-2023  润新知