2014-10-06 34 views
6

我有以下代碼在一個視圖以顯示來自兩個不同的陣列填充兩個表:兩個表迅速

@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重新聲明'。如果我將其替換爲'資金loadView(){'而不是viewdidload應用程序構建。當我測試它時,兩個表都查看與'RFArray'中的數據完全相同的數據。我對編碼非常陌生,無法看到我所做的事情,請幫忙。

回答

11
@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實例實際上正在發送消息。

您無法在派生類中重寫相同的方法兩次。

+1

非常感謝你,這非常有道理,謝謝你的解釋。第一次工作 – samp17 2014-10-06 11:58:32

0

另外確保重新加載每個TableView將數據提取到TableviewCell後。

e.g

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

override func viewDidLoad() { 

    super.viewDidLoad() 
    self.RFTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1") 
    self.IMProdTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2") 

    RFTable.reloadData() 
    IMProdTable.reloadData() 
} 
2

首先創建兩個數據源實現的類 第一個數據源

在視圖控制器

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 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() 
    } 
}