Swift TableView实现冻结窗格功能

时间:2022-01-23 14:11:38

今天做了一个简例,用tableView实现excel冻结窗格功能 Demo:https://git.oschina.net/sunflowrs/FreezePanes.git

初始化Tableview 实现代理

?
1
class BasicTableView:UITableView,UITableViewDelegate,UITableViewDataSource

声明变量时,数组和字典,最好设置成已知类型,避免惹麻烦

?
1
2
var titleArr:Array<String> = []
var listArr:Array<Dictionary<String,String>> = []

改写父类方法时要用override关键字

?
1
2
3
4
5
6
7
override func awakeFromNib() {
  super.awakeFromNib()
  self.delegate = self
  self.dataSource = self
  self.separatorStyle = UITableViewCellSeparatorStyle.none
  self.showsVerticalScrollIndicator = false
}

需要注意:避免爆红 , 一定要实现一下两种方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
 
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/cluy_/article/details/53304051