Swift TableView实现冻结窗格功能
发布时间:2020-12-15 01:10:54 所属栏目:C语言 来源:网络整理
导读:今天做了一个简例,用tableView实现excel冻结窗格功能 Demo:https://git.oschina.net/sunflowrs/FreezePanes.git 初始化Tableview 实现代理 class BasicTableView:UITableView,UITableViewDelegate,UITableViewDataSource 声明变量时,数组和字典,最好设置
今天做了一个简例,用tableView实现excel冻结窗格功能 Demo:https://git.oschina.net/sunflowrs/FreezePanes.git 初始化Tableview 实现代理 class BasicTableView:UITableView,UITableViewDelegate,UITableViewDataSource 声明变量时,数组和字典,最好设置成已知类型,避免惹麻烦 var titleArr:Array<String> = [] var listArr:Array<Dictionary<String,String>> = [] 改写父类方法时要用override关键字 override func awakeFromNib() { super.awakeFromNib() self.delegate = self self.dataSource = self self.separatorStyle = UITableViewCellSeparatorStyle.none self.showsVerticalScrollIndicator = false } 需要注意:避免爆红 , 一定要实现一下两种方法 func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { return titleArr.count } func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { var identifierStr :String if tableView.tag == 1001 { identifierStr = "tableViewCell" }else{ identifierStr = "tableViewCell2" } var cell:UITableViewCell = UITableViewCell.init() if cell.isEqual(nil){ cell = UITableViewCell.init(style: UITableViewCellStyle.default,reuseIdentifier: identifierStr) } if indexPath.row%2 == 0{ cell.backgroundColor = #colorLiteral(red: 1.0,green: 1.0,blue: 1.0,alpha: 1.0) }else{ cell.backgroundColor = #colorLiteral(red: 0.8039215803,green: 0.8039215803,blue: 0.8039215803,alpha: 1) } if tableView.tag == 1001 { cell.textLabel!.text = String(describing: titleArr[ indexPath.row]) cell.textLabel?.textAlignment = NSTextAlignment.center cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 14) if indexPath.row == 0 { cell.textLabel?.textColor = UIColor (colorLiteralRed: 16/255.0,green: 86/255.0,blue: 186/255.0,alpha: 1.0) }else{ cell.textLabel?.textColor = #colorLiteral(red: 0,green: 0,blue: 0,alpha: 1) } }else{ for (idx,value) in listArr.enumerated() { let label:UILabel = UILabel.init(frame: CGRect(x:idx*60,y:0,width:60,height: Int(cell.frame.size.height))) label.textAlignment = NSTextAlignment.center label.font = UIFont.boldSystemFont(ofSize: 14.0) if indexPath.row == 0 { label.textColor = UIColor (colorLiteralRed: 16/255.0,alpha: 1.0) }else{ label.textColor = #colorLiteral(red: 0,alpha: 1) } cell.contentView .addSubview(label) if idx == listArr.count-1{ cell.frame = CGRect(x:cell.frame.origin.x,y:cell.frame.origin.y,width:label.frame.origin.x+label.frame.size.width,height:cell.frame.size.height) } switch (indexPath.row) { case 0: label.text = value["date"]! + "日" case 1: label.text = value["total"] case 2: label.text = value["projectCount"] case 3: label.text = value["visitrate"] case 4: label.text = value["abandonCount"] case 5: label.text = value["abandonrate"] default: label.text = "" } } } return cell; } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |