zoukankan      html  css  js  c++  java
  • 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 "删除"
        }
        
    }
    
  • 相关阅读:
    ASP.NET 缓存 @ OutputCache
    转:Windows下JDK1.6.0+Tomcat6.0的安装配置
    bit、byte、位、字节、汉字、字符之间的区别
    JDK1.6 tomcat 6.0环境变量配置
    DataList和Repeater里的自定义button控件的使用
    Button控件的onclick、oncommand、commandname、CommandArgument的区别
    GridView里的Button控件用法
    ASP.NET2.0 文本编辑器FCKeditor用法
    应用系统架构设计补全篇
    java.util.vector中的vector的详细用法
  • 原文地址:https://www.cnblogs.com/baidaye/p/9323724.html
Copyright © 2011-2022 走看看