2014-10-06 11 views
6

Ho il seguente codice per visualizzare due tabelle popolate da due array diversi in un'unica vista:Due tavoli su una vista in rapida

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

ho ottenuto il primo tavolo di lavoro, e poi copiato e incollato il testo, sostituendo i nomi degli array e i nomi delle tabelle e collegando il delegato e l'origine dati. Tuttavia Xcode visualizza la "redeclaration non valida di viewdidload" sul secondo codice (incollato). Se lo sostituisco con 'fund loadView() {' invece di viewdidload i build dell'app. Tuttavia, quando eseguo il test, entrambe le tabelle visualizzano esattamente gli stessi dati dei dati in "RFArray". Sono MOLTO nuovo nella programmazione e non riesco a vedere cosa ho fatto, per favore aiuto.

risposta

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

Solo una modifica rapida. È necessario mantenere identici i metodi dei delegati e dell'origine dati e controllare quale istanza TableView sta effettivamente inviando il messaggio.

Non è possibile eseguire l'override dello stesso metodo due volte in una classe derivata.

+1

Grazie mille, ha perfettamente senso, grazie per la spiegazione. Ha funzionato la prima volta – samp17

0

Assicurarsi inoltre di ricaricare ogni TableView dopo aver recuperato i dati su TableviewCell.

es

@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

Innanzitutto creare due classi DataSource implementato fonte First Data

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

Secondo dati

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

Set DataSource per Tableview in ViewController

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