Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift中一个视图上的两个表_Swift_Uitableview_Ios8 - Fatal编程技术网

swift中一个视图上的两个表

swift中一个视图上的两个表,swift,uitableview,ios8,Swift,Uitableview,Ios8,我使用以下代码在一个视图中显示从两个不同数组填充的两个表: @IBOutlet var RFTable: UITableView func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { } override func viewDidLoad() { super.viewDidLoad() self.RFTable

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

@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”中的数据完全相同。我对编码非常陌生,看不到我做了什么,请帮助

@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实例实际发送消息


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

还要确保在将数据提取到TableviewCell后重新加载每个TableView

e、 g


首先创建两个数据源实现的类 第一数据源

class FirstDataSouce: NSObject,UITableViewDataSource,UITableViewDelegate {

    var items: [String] = []


   override init(){
       super.init()
   }

   func setData(items:[String]){
       self.items = items
   }



  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecentTableViewCell") as! RecentTableViewCell

        cell.titleLabel.text = items[indexPath.row]

    return cell
  }
}
class SecondDataSouce: NSObject,UITableViewDataSource,UITableViewDelegate {

    var items: [String] = []


   override init(){
       super.init()
   }

  func setData(items:[String]){
      self.items = items
  }



  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return items.count
  }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecentTableViewCell") as! RecentTableViewCell

        cell.titleLabel.text = items[indexPath.row]

     return cell
 }
}
第二数据源

class FirstDataSouce: NSObject,UITableViewDataSource,UITableViewDelegate {

    var items: [String] = []


   override init(){
       super.init()
   }

   func setData(items:[String]){
       self.items = items
   }



  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecentTableViewCell") as! RecentTableViewCell

        cell.titleLabel.text = items[indexPath.row]

    return cell
  }
}
class SecondDataSouce: NSObject,UITableViewDataSource,UITableViewDelegate {

    var items: [String] = []


   override init(){
       super.init()
   }

  func setData(items:[String]){
      self.items = items
  }



  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return items.count
  }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecentTableViewCell") as! RecentTableViewCell

        cell.titleLabel.text = items[indexPath.row]

     return cell
 }
}
在ViewController中将数据源设置为tableview

class ViewController: UIViewController{
    @IBOutlet weak var tableView1: UITableView!
    @IBOutlet weak var tableView2: UITableView!

    var dataSource1: FirstDataSouce!
    var dataSource2: SecondDataSouce!

    func prepareTableViews(){

        let items1 = [“a”,”b”,”c”]
        dataSource1 = FirstDataSouce()
        dataSource1.setData(items: items1)
        self.tableView1.dataSource = dataSource1
        self.tableView1.delegate = dataSource1
        self.tableView1.register(SelectorTableViewCell.self,
                                   forCellReuseIdentifier: 
                                                     "TableViewCell")
        self.tableView1.tableFooterView = UIView()

        let items2 = [“1”,”2”,”3”]
        dataSource2 = SecondDataSouce()
        dataSource2.setData(items: items2)
        self.recentTableView.dataSource = dataSource2
        self.recentTableView.delegate = dataSource2
        self.recentTableView.register(RecentTableViewCell.self,
                                          forCellReuseIdentifier: 
                                                     "TableViewCell")
        self.recentTableView.tableFooterView = UIView()
    }
}

非常感谢,这很有道理,谢谢你的解释。第一次工作