Reputation:
My code below is a typical tableview. What I would like to do is have two tableviews with there respective cells cell and dx both display the var text. I tried combining things using "return cell && cc" but that is not working. I just want to be able to use to separate cells within 2 different tableviews to display the same variable in the same view controller.
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
}}
Upvotes: 1
Views: 580
Reputation: 2252
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!
}
Upvotes: 0
Reputation: 175
you can use two tableview as follows:->
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
}
}
Upvotes: 2