Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

uitableview - Two tables on one view in 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
    }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
@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  
    }
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...