Skip to content

Instantly share code, notes, and snippets.

@mischa-hildebrand
Last active October 24, 2018 15:12
Show Gist options
  • Save mischa-hildebrand/dbea95925615ad984b3972fc8d7f1a29 to your computer and use it in GitHub Desktop.
Save mischa-hildebrand/dbea95925615ad984b3972fc8d7f1a29 to your computer and use it in GitHub Desktop.
Messages Data Source
extension MessagingDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let message = messages[indexPath.row]
let identifier = cellIdentifier(for: message)
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! MessageCell
cell.message = message.text
return cell
}
private func cellIdentifier(for message: Message) -> String {
switch communicationDirection(for: message) {
case .received:
return receivedMessageCellIdentifier
case .sent:
return sentMessageCellIdentifer
}
}
private func communicationDirection(for message: Message) -> Message.CommunicationDirection {
if message.senderId == userId {
return .received
} else {
return .sent
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment