2017-09-20 1 views
1

Mon code ci-dessous est une vue de table typique. Ce que je voudrais faire est d'avoir deux vues de table avec leurs cellules respectives et dx affichent le texte var. J'ai essayé de combiner des choses en utilisant "retour cellule & & cc" mais cela ne fonctionne pas. Je veux juste être en mesure d'utiliser pour séparer les cellules dans 2 tablesviews différentes pour afficher la même variable dans le même contrôleur de vue.utiliser 2 vues de table dans la même vue controller (swift3)

 import UIKit 

var text = ["a", "b"] 

var row = 0 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet var b: UITableView! 
    @IBOutlet var a: UITableView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if tableView == a { 


      return 5 
     } 
     else { 

      return 10 
     } 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     if tableView == a { 
      let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 
      return cell 
     } 
     else { 
      let cell = UITableViewCell(style: .default, reuseIdentifier: "dx") 
      return cell 

     } 
    } 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
row = indexPath.row 
}} 

enter image description here

Répondre

2

vous pouvez utiliser deux tableview comme suit: ->

IBOutlet

@IBOutlet var table1: UITableView! 
@IBOutlet var table2: UITableView! 

UITableViewDataSource, UITableViewDelegate

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == table1 { 
     return 5 
    } 
    else { 
     return 10 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if tableView == table1 { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 
     return cell 
    } 
    else { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: "cell1") 
     return cell 

    } 
} 
+0

Je modifié le code avec votre suggestion qu'il construit. Cependant, le texte var n'est affiché sur aucune des textviews. –

+0

Vos suggestions de code sont reflétées dans ma question éditée ci-dessus. –

0

UITableViewDataSource

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var count:Int? 

    if tableView == self.tblGuests { 
     count = 2 
    } 

    if tableView == self.tblDetail{ 
     count = 5 
    } 

    return count! 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     var cell:UITableViewCell? 

     if tableView == self.tblGuests { 
      cell = tableView.dequeueReusableCell(withIdentifier: "BookingGuestsCell", for: indexPath) as! BookingGuestsCell 
     } else { 
      cell = tableView.dequeueReusableCell(withIdentifier: "BookingRoomDetailCell", for: indexPath) as! BookingRoomDetailCell 
     } 
     return cell! 
}