Reputation: 198
I. I have created a few tableviews in my viewcontroller using a for loop
for index in 0...array.count - 1 {
if let table = Bundle.main.loadNibNamed("Table", owner: self, options: nil)?.first as? Table {
table.frame.origin.y = 200 * CGFloat(index) + 100
table.dataSource = self
table.delegate = self
table.register(UINib.init(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "Cell")
table.isScrollEnabled = false
self.view.addSubview(table)
}
}
II. Now I want to specify the number of rows for each table separately. Apparently it is done like this:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of items in the sample data structure.
var count:Int?
if tableView == self.tableView {
count = sampleData.count
}
if tableView == self.tableView1 {
count = sampleData1.count
}
return count!
}
The example refers to the two different tableviews using self.tableView and self.tableView1. How can I refer to a specific tableview, which was created in my for loop? They were both created as 'table' and I have not stored them under unique variables.
Upvotes: 0
Views: 39
Reputation: 437622
I'd suggest creating a property to hold your Table
instances:
var tables: [Table]?
Then your routine can populate this array:
tables = (0..<array.count).compactMap { index -> Table? in
guard let table = Bundle.main.loadNibNamed("Table", owner: self)?.first as? Table else {
return nil
}
table.frame.origin.y = 200 * CGFloat(index) + 100
table.dataSource = self
table.delegate = self
table.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "Cell")
table.isScrollEnabled = false
return table
}
tables?.forEach { view.addSubview($0) }
Then you can use that array of tables
in your various data source methods.
Upvotes: 1