TableView DataSource Methods with custom cells.
//MARK: - TableView DataSource Methods
//TODO: Declare cellForRowAtIndexPath here:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomMessageCell
//safe alternative
guard let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as? CustomMessageCell
else {fatalError("Could not create the cell")}
cell.messageBody.text = messageArray[indexPath.row].messageBody
cell.senderUsername.text = messageArray[indexPath.row].sender
cell.avatarImageView.image = UIImage(named: "egg")
if cell.senderUsername.text == Auth.auth().currentUser?.email as String! {
cell.avatarImageView.backgroundColor = UIColor.flatMint()
cell.messageBackground.backgroundColor = UIColor.flatSkyBlue()
}else{
cell.avatarImageView.backgroundColor = UIColor.flatWatermelon()
cell.messageBackground.backgroundColor = UIColor.flatGray()
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(itemArray[indexPath.row])
tableView.deselectRow(at: indexPath, animated: true)
}
//TODO: Declare numberOfRowsInSection here:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageArray.count
}