Display Data in Two Different Tableview in the Same View in Swift

Display data in two different TableView in the same View in Swift

Just add two Containers to your View Controller and drag from the container view to your desired Table View Controller and click embed. Then you'll need to make two separate UITableViewController Cocoa files and assign them to the table view in your Identity Inspector.

In this picture I've already attached the container on the left to my Table View Controller on the left.

Sample Image

Two Table Views In Different View Controllers one working and one not(Swift 4)

Your class PantryListTabViewController is already conforming to both TableView Protocols (UITableViewDelegate and UITableViewDataSource)

These protocols tell other classes that this class (PantryListTabViewController) has implemented some specific methods or properties

However, the TableView needs to know, that this class (PantryListTabViewController) is the class the TableView should use to populate it's content and some other things.

Therefore, in your viewDidLoad(), you need to set up your class as the Delegate and DataSource for the TableView:

override func viewDidLoad() {

// Call the super method.
super.viewDidLoad()

// Setup Delegate and DataSource for the TableView.
tableView.delegate = self
tableView.dataSource = self
}

Please consider that you need an IBOutlet of your TableView called tableView (you can obviously rename it)

Hope this helps!

Multiple TableViews in One UIView Swift - How to show both

Unfortunately in my case it was due to my constraints as I had put them into a stack view but with the stack view not set to distribute correctly. So I needed to amend that setting in the storyboard. Then both were present and correct as expected.

use 2 tableviews in the same view controller (swift3)

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

}
}

Table view same cellForRowAt usage for two different cell

It would be much easier if you setup your table view with two sections. The first section would have one row for the cell with the collection view. The second section would have the rows for the elements of your foodNames array.

func numberOfSections(in tableView: UITableView) -> Int {
return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? 1 : foodNames.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainFoodTableViewCell", for: indexPath) as! MainFoodTableViewCell

cell.mainFoodCollectionView.delegate = self
cell.mainFoodCollectionView.dataSource = self
cell.mainFoodCollectionView.reloadData()
cell.mainFoodCollectionView.tag = indexPath.row

return cell
} else {
// Use your actual id and cell class name
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodNameCell", for: indexPath) as! FoodNameCell
cell.textLabel?.text = foodNames[indexPath.row].title

return cell
}
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.section == 0 ? 130 : 44
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.section == 0 ? 100 : 44
}

Show two different custom cells in same uitableview - swift firebase

@Callam's answer is great if you want to put them in two sections.

This is the solution if you want all to be in one section.

First, in numberOfRowsInSection method you need to return the sum of those two array counts like this: return (updates.count + updatesTask.count)

Then you need to configure cellForRowAtIndexPath method like this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if indexPath.row < updates.count{
// Updates
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
let update = updates[indexPath.row]
cell.nameLabel.text = update.addedByUser
return cell
} else {
// UpdatesTask
let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell
let updateTask = updatesTask[indexPath.row-updates.count]
cellTask.nameLabel.text = updateTask.addedByUser
return cellTask
}

}

This will display all cells followed by all cellTasks.


If updates array and updatesTask array have equal number of items and you want to display them one by one you can use this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if indexPath.row % 2 == 0 {
// Updates
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
let update = updates[indexPath.row/2]
cell.nameLabel.text = update.addedByUser
return cell
} else {
// UpdatesTask
let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell
let updateTask = updatesTask[indexPath.row/2]
cellTask.nameLabel.text = updateTask.addedByUser
return cellTask
}

}


Related Topics



Leave a reply



Submit