以前寫本身項目內部的應用調試工具箱:JXCaptain的時候。須要一個顯示數據庫的視圖,須要有行列表頭固定,而且表單內容能夠上下左右滑動查看的特性,就像Excel視圖同樣。整個功能就只有一個ExcelView類,邏輯清晰簡單,便於閱讀和修改定製需求。git
JXExcelgithub
ExcelView
excel = ExcelView(frame: CGRect.zero)
excel.dataSource = self
excel.delegate = self
view.addSubview(excel)
複製代碼
ExcelViewDataSource
代理方法func numberOfRows(in excelView: ExcelView) -> Int {
return 50
}
func numberOfColumns(in excelView: ExcelView) -> Int {
return 10
}
func excelView(_ excelView: ExcelView, columnNameAt column: Int) -> String {
return "col:\(column)"
}
func excelView(_ excelView: ExcelView, rowDataAt row: Int) -> [String] {
return dataSource[row]
}
func excelView(_ excelView: ExcelView, rowHeightAt row: Int) -> CGFloat {
return 40
}
func excelView(_ excelView: ExcelView, columnWidthAt column: Int) -> CGFloat {
return 120
}
func widthOfLeftHeader(in excelView: ExcelView) -> CGFloat {
return 50
}
func heightOfTopHeader(in excelView: ExcelView) -> CGFloat {
return 40
}
複製代碼
ExcelViewDelegate
代理方法func excelView(_ excelView: ExcelView, didTapGridWith content: String) {
print("didTapGridWith:\(content)")
}
func excelView(_ excelView: ExcelView, didTapColumnHeaderWith name: String) {
print("didTapColumnHeaderWith:\(name)")
}
複製代碼