swift中UITableView的使用(常规使用)

前端之家收集整理的这篇文章主要介绍了swift中UITableView的使用(常规使用)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

源码:https://github.com/potato512/SYSwiftLearning



  1. // MARK: - 初始化tableview
  2. func setUI()
  3. {
  4. // 初始化tableView
  5. self.mainTableView = UITableView(frame:self.view.bounds,style:UITableViewStyle.Plain)
  6. self.view.addSubview(self.mainTableView!)
  7. // 设置tableView的数据源
  8. self.mainTableView!.dataSource = self
  9. // 设置tableView的委托
  10. self.mainTableView!.delegate = self
  11. // 背景颜色
  12. self.mainTableView.backgroundColor = UIColor.clearColor()
  13. // 背景图片
  14. let bgroundView = UIImageView(frame: self.view.bounds)
  15. bgroundView.image = UIImage(named: "01")
  16. self.mainTableView.backgroundView = bgroundView
  17. // 其他属性
  18. self.mainTableView.scrollEnabled = true
  19. self.mainTableView.scrollsToTop = true
  20. // 去掉底端多余分割线,即表尾视图
  21. let footerLabel = UILabel(frame: CGRectMake(0.0,0.0,CGRectGetWidth(self.mainTableView.frame),100.0))
  22. footerLabel.backgroundColor = UIColor.greenColor()
  23. footerLabel.text = "列表视图的表尾视图"
  24. footerLabel.textAlignment = NSTextAlignment.Center
  25. self.mainTableView.tableFooterView = footerLabel
  26. // 表头视图
  27. let headerLabel = UILabel(frame: CGRectMake(0.0,40.0))
  28. headerLabel.backgroundColor = UIColor.brownColor()
  29. headerLabel.text = "列表视图的表头视图"
  30. headerLabel.textAlignment = NSTextAlignment.Center
  31. self.mainTableView.tableHeaderView = headerLabel
  32. }

  1. // MARK: - 响应事件
  2. func buttonClick(button:UIBarButtonItem) -> Void
  3. {
  4. let index = button.tag
  5. if 1 == index
  6. {
  7. // 返回顶部
  8. self.mainTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0,inSection: 0),atScrollPosition: UITableViewScrollPosition.Top,animated: true)
  9. }
  10. else if 2 == index
  11. {
  12. // 回到中间
  13. self.mainTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: (self.mainArray.count - 1) / 2,atScrollPosition: UITableViewScrollPosition.Middle,animated: true)
  14. }
  15. else if 3 == index
  16. {
  17. // 去到底部
  18. self.mainTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: (self.mainArray.count - 1),atScrollPosition: UITableViewScrollPosition.Bottom,animated: true)
  19. }
  20. }

  1. // MARK: - UITableViewDataSource,UITableViewDelegate
  2. func numberOfSectionsInTableView(tableView: UITableView) -> Int
  3. {
  4. return 1
  5. }
  6. func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int
  7. {
  8. return self.mainArray.count
  9. }
  10. func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  11. {
  12. // var cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")! // 此写法异常
  13. var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")
  14. if (cell == nil)
  15. {
  16. cell = UITableViewCell(style: UITableViewCellStyle.Value1,reuseIdentifier: "UITableViewCell")
  17. cell.textLabel!.textColor = UIColor.blackColor()
  18. cell.textLabel!.text = "当前字体"
  19. cell.textLabel!.font = UIFont.systemFontOfSize(12.0)
  20. cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
  21. cell.detailTextLabel!.font = UIFont.systemFontOfSize(12.0)
  22. }
  23.  
  24. // cell附件类型
  25. cell.accessoryType = UITableViewCellAccessoryType.None
  26. switch indexPath.row
  27. {
  28. case 0:
  29. cell.accessoryType = UITableViewCellAccessoryType.Checkmark
  30. break
  31. case 1:
  32. cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
  33. break
  34. case 2:
  35. cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
  36. break
  37. case 3:
  38. cell.accessoryType = UITableViewCellAccessoryType.DetailButton
  39. break
  40. case 4:
  41. let imageview = UIImageView(image: UIImage(named: "normalImage"))
  42. imageview.frame = CGRectMake(0.0,30.0,30.0)
  43. cell.accessoryView = imageview
  44. break
  45. default:
  46. cell.accessoryType = UITableViewCellAccessoryType.DetailButton
  47. break
  48. }
  49. // cell图标
  50. cell.imageView!.image = UIImage(named: "normalImage")
  51. let text:String = self.mainArray[indexPath.row as Int] as! String
  52. cell.detailTextLabel!.text = text
  53. return cell
  54. }
  55. func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath)
  56. {
  57. tableView.deselectRowAtIndexPath(indexPath,animated: true)
  58. }
  59.  
  60. // MARK: - 附件类型
  61. // cell类型为 DetailDisclosureButton,或DetailButton 时的点击响应事件
  62. func tableView(tableView: UITableView,accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
  63. print("点击了:\(indexPath.row)")
  64. }
  65. // cell附件类型(注意:不能同时设置cell.accessoryType = UITableViewCellAccessoryType.None)
  66. // func tableView(tableView: UITableView,accessoryTypeForRowWithIndexPath indexPath: NSIndexPath) -> UITableViewCellAccessoryType {
  67. // return UITableViewCellAccessoryType.Checkmark
  68. // }

猜你在找的Swift相关文章