swift中的一个视图上的两个表

前端之家收集整理的这篇文章主要介绍了swift中的一个视图上的两个表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码显示在一个视图中从两个不同数组填充的两个表:
  1. @IBOutlet var RFTable: UITableView
  2. func tableView(tableView: UITableView!,didSelectRowAtIndexPath indexPath: NSIndexPath!) {
  3.  
  4. }
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7.  
  8. self.RFTable.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")
  9. }
  10. func tableView(tableView: UITableView!,numberOfRowsInSection section: Int) -> Int {
  11. return self.RFArray.count;
  12. }
  13. func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
  14. var cell:UITableViewCell = self.RFTable.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
  15.  
  16. cell.textLabel.text = String(self.RFArray[indexPath.row])
  17.  
  18. return cell
  19. }
  20.  
  21. @IBOutlet var IMProdTable: UITableView
  22. func tableView2(IMProdTable: UITableView!,didSelectRowAtIndexPath indexPath: NSIndexPath!) {
  23.  
  24. }
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27.  
  28. self.IMProdTable.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell2")
  29. }
  30. func tableView2(IMProdTable: UITableView!,numberOfRowsInSection section: Int) -> Int {
  31. return self.IMProdArray.count;
  32. }
  33. func tableView2(IMProdTable: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
  34. var cell2:UITableViewCell = self.IMProdTable.dequeueReusableCellWithIdentifier("cell2") as UITableViewCell
  35.  
  36. cell2.textLabel.text = String(self.IMProdArray[indexPath.row])
  37.  
  38. return cell2
  39. }

我得到了第一个表,然后复制并粘贴了文本,替换了数组名称和tableview名称,并连接了委托和数据源.但是,Xcode在第二个(粘贴的)代码显示“viewdidload的无效重新声明”.如果我将此替换为’fund loadView(){‘而不是viewdidload应用程序构建.当我测试它时,两个表都查看完全相同的数据,即’RFArray’中的数据.我是非常新的编码,看不到我做了什么,请帮忙.

  1. @IBOutlet var RFTable: UITableView
  2. @IBOutlet var IMProdTable: UITableView
  3.  
  4. func tableView(tableView: UITableView!,didSelectRowAtIndexPath indexPath: NSIndexPath!) {
  5.  
  6. }
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10.  
  11. self.RFTable.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")
  12. self.IMProdTable.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell2")
  13. }
  14.  
  15. func tableView(tableView: UITableView!,numberOfRowsInSection section: Int) -> Int {
  16. if tableView == RFTable {
  17. return self.RFArray.count;
  18. } else {
  19. return self.IMProdArray.count;
  20. }
  21. }
  22.  
  23. func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
  24. if tableView == RFTable {
  25. var cell:UITableViewCell = self.RFTable.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
  26. cell.textLabel.text = String(self.RFArray[indexPath.row])
  27. return cell
  28. } else {
  29. var cell2:UITableViewCell = self.IMProdTable.dequeueReusableCellWithIdentifier("cell2") as UITableViewCell
  30. cell2.textLabel.text = String(self.IMProdArray[indexPath.row])
  31. return cell2
  32. }
  33. }

只需快速编辑.您需要保持委托和数据源方法相同,并检查哪个TableView实例实际发送消息.

您不能在派生类中两次覆盖相同的方法.

猜你在找的Swift相关文章