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
}
}
Multiple Custom Cells in UITableView With Firebase Database
You implement cellForRow
twice , you have to
var itemsArr = [Item]()
//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = itemsArr[indexPath.row]
if item.isDate {
let augustDate = tableView.dequeueReusableCell(withIdentifier: "augustDate") as! dateCell
augustDate.date.text = item.content
return augustDate
}
else {
let augustEvents = tableView.dequeueReusableCell(withIdentifier: "augustEvents") as! eventCell
augustEvents.even.text = item.content
return augustEvents
}
}
//
then make it one array and append items of this struct type
struct Item {
var isDate:Bool
var content:String
}
iOS Two Cells in one Row With Data
For this You can use UICollection view controller
.
Or If you want to use the Table view
then create the custom cell
as per your requirement.
Generic UITableView
You should create custom cells in your table view and then you need to detect which type of cell you want to use and after that set cells.
Let´s say that you have three type of cells:
- A cell with a
Switch
(custom) - A cell with a
textField
(custom) - A regular cell (regular)
In your cellForRowAt
you need to detect which kind of cell you want to use. You can either do that by checking the indexPath.row
if you have static positions or you can check the data type in your array and set the cell depending on that.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchCell", for: indexPath) as! SwitchCell
// set cell values here
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell", for: indexPath) as! TextFieldCell
// set cell values here
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell
// set cell values here
return cell
}
}
Here is a good guide of how you can create your custom cells and you can have as many custom cells as you want.
Custom uitableview cell not showing all text labels
You are using the standard UITableViewCell
and you assign all three values to the same label.
You have to cast the cell to the custom cell and assign the values to the custom labels
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let use a hack for now, we actually need to dequeue our cells for memory efficiency
// let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
let user = users[indexPath.row]
cell.nameLabel?.text = user.date_clasname
cell.teacherLabel?.text = user.teacher
cell.roomLabel?.text = user.room_number
return cell
}
Replace nameLabel
, teacherLabel
and roomLabel
with the real property names.
And please conform to the naming convention and name variables lowerCamelCased for example dateClasname
and roomNumber
Custom Cell with 4 Textfields
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var simpleTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SimpleCell", for: indexPath) as! SimpleCell
if(indexPath.row % 2 == 0 ) {
cell.vi1.backgroundColor = .red
} else {
cell.vi1.backgroundColor = .black
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
class SimpleCell: UITableViewCell {
@IBOutlet weak var vi1: UIView!
@IBOutlet weak var vi2: UIView!
override func awakeFromNib() {
super.awakeFromNib()
// things done
}
}
Use storyboard connection as @IBOutlets
Drag & drop UITableView
to UIViewController
as setting delegate
& dataSource
Select your custom UITableViewCell in storyboard and in Identity Inspector
set its cell class to your UITableViewCell
class name
Firebase database not pulling data to custom cell in tableview
You have two data arrays named "posts." The outer, object scope one, never gets set. Remove the inner one.
Related Topics
Why am I Getting Com.Facebook.Sdk.Login Error 308
How to Add .Plist File to All Targets in Xcode
Use More Than One Firebase Database in Single App - Swift
App Crash on Device But Works on Simulator iOS
What Are the First Two Columns in Scnmatrix4
Manage a Uipickerview from an External Class - Using Swift
Firebase Dynamic Link Doesnt Work in Safari Swift
How to Implement a Payment App with Braintree in iOS
Creating Tableview Sections from JSON Data Swift 4
How to Set Clear Background in Table View Cell Swipe Action
Auto Focus and Auto Exposure in Avfoundation on Custom Camera Layer
How to Create a Pfquerytableviewcontroller with Parse in Swift
Swift:How to Convert String to String with Time Format
iOS Keyboard Active But Invisible When Uisearchbar Is Tapped