Last active
October 24, 2018 15:12
-
-
Save mischa-hildebrand/dbea95925615ad984b3972fc8d7f1a29 to your computer and use it in GitHub Desktop.
Messages Data Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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