TSSimpleTableView.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // TSTableView.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2025/1/4.
  6. //
  7. open class TSSimpleTableView : NSObject{
  8. open var cellID:String = "TSSimpleTableViewCell"
  9. open lazy var tableView:UITableView = {
  10. let tableView = UITableView()
  11. tableView.initBaseTableView(reuseClass: reuseClass, isUseMJRefresh: false, delegate: self)
  12. return tableView
  13. }()
  14. open var reuseClass:[String] = [] {
  15. didSet{
  16. if let string = reuseClass.first {
  17. cellID = string
  18. }
  19. tableView.registerCellClass(cellIds: reuseClass)
  20. }
  21. }
  22. open var dataArray:[TSBasicSectionModel] = [TSBasicSectionModel](){
  23. didSet{
  24. tableView.reloadData()
  25. }
  26. }
  27. }
  28. extension TSSimpleTableView : UITableViewDataSource, UITableViewDelegate {
  29. public func numberOfSections(in tableView: UITableView) -> Int {
  30. return dataArray.count
  31. }
  32. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  33. if let sectionModel = dataArray.safeObj(At: section){
  34. return sectionModel.itemsArray.count
  35. }
  36. return 0
  37. }
  38. public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  39. if let sectionModel = dataArray.safeObj(At: indexPath.section),let itemModel = sectionModel.itemsArray.safeObj(At: indexPath.row){
  40. return itemModel.height
  41. }
  42. return 0
  43. }
  44. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  45. let cell = tableView.dequeueReusableCell(withIdentifier: cellID)
  46. if let cell = cell as? TSSimpleTableViewCell {
  47. if let sectionModel = dataArray.safeObj(At: indexPath.section),let itemModel = sectionModel.itemsArray.safeObj(At: indexPath.row){
  48. cell.itemModel = itemModel
  49. }
  50. return cell
  51. }
  52. return UITableViewCell()
  53. }
  54. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  55. if let sectionModel = dataArray.safeObj(At: indexPath.section),let itemModel = sectionModel.itemsArray.safeObj(At: indexPath.row){
  56. itemModel.tapBlock?(itemModel,indexPath.row,nil)
  57. }
  58. }
  59. }
  60. open class TSSimpleTableViewCell: TSBaseTabViewCell {
  61. open var itemModel:TSBasicItemModel = TSBasicItemModel()
  62. open func didEndDisplaying(indexPath: IndexPath){
  63. }
  64. }