Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  2.  
  3. class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  4. @IBOutlet weak var scroll: UIScrollView!
  5.  
  6. @IBOutlet var recipesImageView: UIImageView!
  7. @IBOutlet var tableView:UITableView!
  8. @IBOutlet var tableView2: UITableView!
  9.  
  10. var recipes:Recipe!
  11. var noodles:Recipe!
  12. var snacks:Recipe!
  13. var category: Int = Int()
  14.  
  15. @IBAction func segmented(sender: AnyObject) {
  16. switch sender.selectedSegmentIndex {
  17. case 0:
  18. scroll.setContentOffset(CGPoint(x:0, y:0), animated: true)
  19. case 1:
  20. scroll.setContentOffset(CGPoint(x:375, y:0), animated: true)
  21. case 2:
  22. scroll.setContentOffset(CGPoint(x:750, y:0), animated: true)
  23. case 3:
  24. scroll.setContentOffset(CGPoint(x:1125, y:0), animated: true)
  25.  
  26.  
  27. default:
  28. print()
  29. }
  30. }
  31.  
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34.  
  35. // Do any additional setup after loading the view.
  36. switch self.category {
  37. case 0:
  38. self.recipesImageView.image = UIImage(named: recipes.image)
  39. title = self.recipes.name
  40. case 1:
  41. self.recipesImageView.image = UIImage(named: noodles.image)
  42. title = self.noodles.name
  43. case 2:
  44. self.recipesImageView.image = UIImage(named: snacks.image)
  45. title = self.snacks.name
  46. case 3:
  47. self.recipesImageView.image = UIImage(named: noodles.image)
  48. title = self.noodles.name
  49. case 4:
  50. self.recipesImageView.image = UIImage(named: noodles.image)
  51. title = self.noodles.name
  52. default:
  53. self.recipesImageView.image = UIImage(named: recipes.image)
  54. title = self.recipes.name
  55. }
  56.  
  57. self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.5)
  58.  
  59. //刪除多餘的隔線
  60. self.tableView.tableFooterView = UIView(frame: CGRectZero)
  61.  
  62. self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1)
  63.  
  64. //title = self.recipes.name
  65.  
  66. tableView.estimatedRowHeight = 36.0;
  67. tableView.rowHeight = UITableViewAutomaticDimension;
  68.  
  69. self.tableView.delegate = self
  70. self.tableView.dataSource = self
  71.  
  72. self.tableView2.delegate = self
  73. self.tableView2.dataSource = self
  74.  
  75. }
  76.  
  77. override func didReceiveMemoryWarning() {
  78. super.didReceiveMemoryWarning()
  79. // Dispose of any resources that can be recreated.
  80. }
  81.  
  82. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  83. var cellNum:Int?
  84.  
  85. if tableView == self.tableView {
  86. cellNum = 4
  87. }
  88. else if tableView == self.tableView2 {
  89. cellNum = 1
  90. }
  91.  
  92. return cellNum!
  93. }
  94.  
  95.  
  96.  
  97. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  98.  
  99.  
  100. if(tableView == self.tableView) {
  101. let cell: DetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! DetailTableViewCell
  102. //cell.backgroundColor = UIColor.clearColor()
  103. if self.category == 0 {
  104. switch indexPath.row {
  105. case 0:
  106. cell.fieldLabel.text = "名稱"
  107. cell.valueLabel.text = recipes.name
  108. cell.amountLabel.text = ""
  109.  
  110. case 1:
  111. cell.fieldLabel.text = "類型"
  112. cell.valueLabel.text = recipes.type
  113. cell.amountLabel.text = ""
  114.  
  115. case 2:
  116. cell.fieldLabel.text = "收藏"
  117. cell.valueLabel.text = (recipes.isFavor) ? "Yes" : "No"
  118. cell.amountLabel.text = ""
  119.  
  120. case 3:
  121. cell.fieldLabel.text = "食材"
  122. cell.valueLabel.text = recipes.material
  123. cell.amountLabel.text = recipes.amount
  124. default:
  125. cell.fieldLabel.text = ""
  126. cell.valueLabel.text = ""
  127. cell.amountLabel.text = ""
  128. }
  129. }
  130.  
  131. if self.category == 1 {
  132. switch indexPath.row {
  133. case 0:
  134. cell.fieldLabel.text = "名稱"
  135. cell.valueLabel.text = noodles.name
  136. cell.amountLabel.text = ""
  137.  
  138. case 1:
  139. cell.fieldLabel.text = "類型"
  140. cell.valueLabel.text = noodles.type
  141. cell.amountLabel.text = ""
  142.  
  143. case 2:
  144. cell.fieldLabel.text = "收藏"
  145. cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
  146. cell.amountLabel.text = ""
  147.  
  148. case 3:
  149. cell.fieldLabel.text = "食材"
  150. cell.valueLabel.text = noodles.material
  151. cell.amountLabel.text = noodles.amount
  152. default:
  153. cell.fieldLabel.text = ""
  154. cell.valueLabel.text = ""
  155. cell.amountLabel.text = ""
  156. }
  157. }
  158.  
  159. if self.category == 2 {
  160. switch indexPath.row {
  161. case 0:
  162. cell.fieldLabel.text = "名稱"
  163. cell.valueLabel.text = snacks.name
  164. cell.amountLabel.text = ""
  165.  
  166. case 1:
  167. cell.fieldLabel.text = "類型"
  168. cell.valueLabel.text = snacks.type
  169. cell.amountLabel.text = ""
  170.  
  171. case 2:
  172. cell.fieldLabel.text = "收藏"
  173. cell.valueLabel.text = (snacks.isFavor) ? "Yes" : "No"
  174. cell.amountLabel.text = ""
  175.  
  176. case 3:
  177. cell.fieldLabel.text = "食材"
  178. cell.valueLabel.text = snacks.material
  179. cell.amountLabel.text = snacks.amount
  180. default:
  181. cell.fieldLabel.text = ""
  182. cell.valueLabel.text = ""
  183. cell.amountLabel.text = ""
  184. }
  185. }
  186.  
  187. if self.category == 3 {
  188. switch indexPath.row {
  189. case 0:
  190. cell.fieldLabel.text = "名稱"
  191. cell.valueLabel.text = noodles.name
  192. cell.amountLabel.text = ""
  193.  
  194. case 1:
  195. cell.fieldLabel.text = "類型"
  196. cell.valueLabel.text = noodles.type
  197. cell.amountLabel.text = ""
  198.  
  199. case 2:
  200. cell.fieldLabel.text = "收藏"
  201. cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
  202. cell.amountLabel.text = ""
  203.  
  204. case 3:
  205. cell.fieldLabel.text = "食材"
  206. cell.valueLabel.text = noodles.material
  207. cell.amountLabel.text = noodles.amount
  208. default:
  209. cell.fieldLabel.text = ""
  210. cell.valueLabel.text = ""
  211. cell.amountLabel.text = ""
  212. }
  213. }
  214.  
  215. if self.category == 4 {
  216. switch indexPath.row {
  217. case 0:
  218. cell.fieldLabel.text = "名稱"
  219. cell.valueLabel.text = noodles.name
  220. cell.amountLabel.text = ""
  221.  
  222. case 1:
  223. cell.fieldLabel.text = "類型"
  224. cell.valueLabel.text = noodles.type
  225. cell.amountLabel.text = ""
  226.  
  227. case 2:
  228. cell.fieldLabel.text = "收藏"
  229. cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
  230. cell.amountLabel.text = ""
  231.  
  232. case 3:
  233. cell.fieldLabel.text = "食材"
  234. cell.valueLabel.text = noodles.material
  235. cell.amountLabel.text = noodles.amount
  236. default:
  237. cell.fieldLabel.text = ""
  238. cell.valueLabel.text = ""
  239. cell.amountLabel.text = ""
  240. }
  241. }
  242. return cell
  243. }
  244. else if tableView == tableView2 {
  245. let cell: MaterialDetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell2") as! MaterialDetailTableViewCell
  246. //cell.backgroundColor = UIColor.clearColor()
  247. if self.category == 0 {
  248. switch indexPath.row {
  249. case 0:
  250. cell.materialLabel.text = recipes.material
  251. default:
  252. cell.materialLabel.text = ""
  253. }
  254. }
  255. if self.category == 1 {
  256. switch indexPath.row {
  257. case 0:
  258. cell.materialLabel.text = noodles.material
  259. default:
  260. cell.materialLabel.text = ""
  261. }
  262. }
  263. if self.category == 2 {
  264. switch indexPath.row {
  265. case 0:
  266. cell.materialLabel.text = snacks.material
  267. default:
  268. cell.materialLabel.text = ""
  269. }
  270. }
  271. if self.category == 3 {
  272. switch indexPath.row {
  273. case 0:
  274. cell.materialLabel.text = noodles.material
  275. default:
  276. cell.materialLabel.text = ""
  277. }
  278. }
  279. if self.category == 4 {
  280. switch indexPath.row {
  281. case 0:
  282. cell.materialLabel.text = noodles.material
  283. default:
  284. cell.materialLabel.text = ""
  285. }
  286. }
  287. return cell
  288. }
  289. //-----------------------this line appear error
  290. }
  291.  
  292.  
  293. //避免被前面一頁的隱藏導覽列所影響
  294. override func viewWillAppear(animated: Bool) {
  295. super.viewWillAppear(animated)
  296.  
  297. self.navigationController?.hidesBarsOnSwipe = false
  298. self.navigationController?.setNavigationBarHidden(false, animated: true)
  299. }
  300.  
  301. /*
  302. // MARK: - Navigation
  303.  
  304. // In a storyboard-based application, you will often want to do a little preparation before navigation
  305. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  306. // Get the new view controller using segue.destinationViewController.
  307. // Pass the selected object to the new view controller.
  308. }
  309. */
  310.  
  311. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  312. if(tableView == self.tableView) {
  313. //returning cell correctly
  314. } else if tableView == tableView2 {
  315. //returning cell correctly
  316. }
  317. //you need to return something here too
  318. }
  319.  
  320. if tableView == self.tableView {
  321. // blah blah blah
  322. return some stuff
  323. } else if tableView == tableView2 {
  324. // blah blah blah
  325. return some stuff
  326. }
  327.  
  328. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  329.  
  330.  
  331. if(tableView == self.tableView) {
  332. let cell: DetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! DetailTableViewCell
  333. //Some code for cell
  334. return cell
  335. }
  336.  
  337. else if tableView == tableView2 {
  338. let cell: MaterialDetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell2") as! MaterialDetailTableViewCell
  339. //Some code for cell
  340. return cell
  341. }
  342.  
  343. //--> Compiler Says: What should I do if both are false. I need to return cell anyways. Please guide me.!!!!
  344. //--> Solution : Return Cell from here as well
  345.  
  346. }
  347.  
  348. if tableView == self.tableView {
  349. // ...
  350. } else if tableView == tableView2 {
  351. // ...
  352. } else {
  353. fatalError("💥 Unexpected behaviour: only 2 tables are supported")
  354. // Or any other assertion...
  355. }
  356.  
  357. return UITableViewCell()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement