首页 > 编程 > Swift > 正文

Swift TableView实现冻结窗格功能

2020-03-09 17:43:23
字体:
来源:转载
供稿:网友

今天做了一个简例,用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, green: 86/255.0, blue: 186/255.0, alpha: 1.0)        }else{          label.textColor = #colorLiteral(red: 0, green: 0, blue: 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;    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到swift教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表