swift中的一个视图上的两个表 [英] Two tables on one view in swift

查看:115
本文介绍了swift中的一个视图上的两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在一个视图中显示从两个不同数组填充的两个表:

I have the following code to display two tables populated from two different arrays in one view:

@IBOutlet var RFTable: UITableView
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.RFTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.RFArray.count;
    }
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->     UITableViewCell! {
        var cell:UITableViewCell = self.RFTable.dequeueReusableCellWithIdentifier("cell") as     UITableViewCell

        cell.textLabel.text = String(self.RFArray[indexPath.row])

        return cell
    }

    @IBOutlet var IMProdTable: UITableView
    func tableView2(IMProdTable: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)     {

    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.IMProdTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2")
    }
    func tableView2(IMProdTable: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return self.IMProdArray.count;
    }
    func tableView2(IMProdTable: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->     UITableViewCell! {
        var cell2:UITableViewCell = self.IMProdTable.dequeueReusableCellWithIdentifier("cell2") as UITableViewCell

        cell2.textLabel.text = String(self.IMProdArray[indexPath.row])

        return cell2
    }

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

I got the first table working, and then copied and pasted the text, replacing the array names and tableview names, and have hooked up the delegate and datasource. However Xcode displays 'invalid redeclaration of viewdidload' on the second (pasted) code. If I replace this to 'fund loadView() {' instead of viewdidload the app builds. When I test it though, both tables view exactly the same data which is the data in 'RFArray.' I am VERY new to coding and cannot see what I have done, please help.

推荐答案

@IBOutlet var RFTable: UITableView
@IBOutlet var IMProdTable: UITableView

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

}

override func viewDidLoad() {
    super.viewDidLoad()

    self.RFTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.IMProdTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2")
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
  if tableView == RFTable {
    return self.RFArray.count;
  } else {
    return self.IMProdArray.count;
  }
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->     UITableViewCell! {
  if tableView == RFTable {
    var cell:UITableViewCell = self.RFTable.dequeueReusableCellWithIdentifier("cell") as     UITableViewCell
    cell.textLabel.text = String(self.RFArray[indexPath.row])
    return cell
  } else {
    var cell2:UITableViewCell = self.IMProdTable.dequeueReusableCellWithIdentifier("cell2") as UITableViewCell
    cell2.textLabel.text = String(self.IMProdArray[indexPath.row])
    return cell2  
    }
}

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

Just a quick edit. You need to keep the delegate and datasource methods same and check which TableView instance is actually sending the message.

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

You cannot override the same method twice in a derived class.

这篇关于swift中的一个视图上的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆