Guest User

Untitled

a guest
Jul 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. extension SearchController: UITableViewDataSource {
  2.  
  3. func numberOfSections(in tableView: UITableView) -> Int {
  4. return 1
  5. }
  6.  
  7. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  8. switch currentSearchType {
  9. case .menuItems:
  10. return menuItemResults.count
  11. case .restaurants:
  12. return restaurantResults.count
  13. }
  14. }
  15.  
  16. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  17. switch currentSearchType {
  18.  
  19. case .menuItems:
  20. let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell") as! SearchItemCell
  21. cell.configure(item: menuItemResults[indexPath.row])
  22. return cell
  23. case .restaurants:
  24. let cell = tableView.dequeueReusableCell(withIdentifier: "RestaurantCell") as! RestaurantCell
  25. cell.configure(restaurant: restaurantResults[indexPath.row])
  26. return cell
  27. }
  28.  
  29. }
  30. }
  31.  
  32.  
  33. class SearchItemCell: UITableViewCell {
  34.  
  35. var ratingPie: PieChart!
  36. var ratingPieLabel: UILabel!
  37.  
  38. required init(coder aDecoder: NSCoder) {
  39.  
  40. super.init(coder: aDecoder)!
  41. }
  42.  
  43.  
  44. override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
  45.  
  46. super.init(style: style, reuseIdentifier: reuseIdentifier)
  47.  
  48. ratingPie = PieChart()
  49. ratingPie.layers = []
  50. […, ratingPie, …].forEach {
  51. contentView.addSubview($0)
  52. }
  53.  
  54. ratingPieLabel = UILabel()
  55. [ratingPieLabel].forEach {
  56. ratingPie.addSubview($0)
  57. }
  58. ratingPie.anchor(top: nil, leading: nil, bottom: bottomAnchor, trailing: trailingAnchor,
  59. padding: .init(top: 0, left: 0, bottom: 16, right: 8),
  60. size: .init(width: 40.0, height: 40))
  61. ratingPie.animDuration = 0
  62. ratingPie.referenceAngle=210
  63. ratingPie.innerRadius=14
  64. ratingPie.outerRadius=18
  65.  
  66. ratingPieLabel.anchorCenter(to: ratingPie)
  67. ratingPieLabel.font = UIFont(name: StyleKit.Font.regular, size: 10)
  68.  
  69. func configure(item: MenuItem) {
  70. if item.overallRating > 0{
  71. ratingPie.isHidden = false
  72. ratingPieLabel.isHidden = false
  73. ratingPie.models = [PieSliceModel(value: (1-item.overallRating)*10, color: .lightGray),
  74. PieSliceModel(value: item.overallRating*10, color: .green)]
  75. ratingPieLabel.text = String(format: "%.0f", item.overallRating * 100)
  76. } else{
  77. ratingPie.isHidden = true
  78. ratingPieLabel.isHidden = true
  79. }
  80. }
  81. }
Add Comment
Please, Sign In to add comment