swift - swift 一个 View 上的两个表

标签 swift uitableview ios8

我有以下代码在一个 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 名称,并连接委托(delegate)和数据源。但是,Xcode 在第二个(粘贴的)代码上显示“无效的 viewdidload 重新声明”。如果我将其替换为 'fund loadView() {' 而不是 viewdidload 应用程序构建。但是,当我对其进行测试时,两个表都查看完全相同的数据,即“RFArray”中的数据。我对编码很陌生,看不到我做了什么,请帮忙。

最佳答案

@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  
    }
}

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

您不能在派生类中重写同一个方法两次。

关于swift - swift 一个 View 上的两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26215475/

相关文章:

swift - 如何在 Xcode 6 中模拟应用程序的首次启动

swift - 从 UIView 显示 AlertView?

ios - 距顶部布局指南和底部布局指南的距离相等

swift - 以速度旋转 scnnode

ios - 从模态解雇中强制 tableView 重新加载数据?

swift - 在 swift 中将文本从 UITextField 发送到短信

iOS 13 VoIP 推送通知-应用程序终止时未调用 didReceiveIncomingPushWith

ios - 更改表格宽度时,Swift Tableview 会向上移动

ios - Storyboard - 回合制游戏的工作流程

objective-c - 具有活力的 UITextField 占位符