import UIKit
class MessageDetailViewController: UIViewController {
@IBOutlet weak var tableview: UITableView!
struct data{
let time: String
let message: String
let date: String
let type: String
}
struct section {
let date: String
var array = [data]()
}
let sectionArray = [
section(date: "yesterday", array: [
data(time: "3:30 PM", message: "I am looking for your service, can you please give more information on that.", date: "yesterday", type: "sender"),
data(time: "3:40 PM", message: "Sure i am here to help you", date: "yesterday", type: "receiver")
]),
section(date: "today", array: [data(time: "4:40 PM", message: "Ok, I wil contact you on your phone for that.", date: "today", type: "sender")])
]
override func viewDidLoad() {
super.viewDidLoad()
self.tableview.register(UINib(nibName: "SenderCell", bundle: nil), forCellReuseIdentifier: "SenderCell")
self.tableview.register(UINib(nibName: "ReceiverCell", bundle: nil), forCellReuseIdentifier: "ReceiverCell")
self.tableview.register(UINib(nibName: "customHeaderViewCell", bundle: nil), forCellReuseIdentifier: "customHeaderViewCell")
}
}
extension MessageDetailViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if sectionArray[indexPath.section].array[indexPath.row].type == "sender"
{
let cell = tableview.dequeueReusableCell(withIdentifier: "SenderCell", for: indexPath) as! SenderCell
cell.setData(reciverTime: sectionArray[indexPath.section].array[indexPath.row].time, reciverMsg: sectionArray[indexPath.section].array[indexPath.row].message)
return cell
}
else
{
let cell = tableView.dequeueReusableCell(withIdentifier: "ReceiverCell", for: indexPath) as! ReceiverCell
cell.setData(reciverTime: sectionArray[indexPath.section].array[indexPath.row].time, reciverMsg: sectionArray[indexPath.section].array[indexPath.row].message)
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let headerCell = tableView.dequeueReusableCell(withIdentifier: "customHeaderViewCell") as! customHeaderViewCell
if sectionArray[section].date == "yesterday"{
headerCell.labelHeader.text = "yesterday"
}
else{
headerCell.labelHeader.text = "today"
}
headerView.addSubview(headerCell)
return headerView
}
}
This is my message detail code.
I want to achieve these
But through these array I achieved these
what changes should I do in my code so that I can get same day message in one section
this ---
sectionArray.append(section(date: "yesterday", array: [data(time: "3:40 PM", message: "SURE i am here to help you", date: "yesterday", type: "receiver")]))
Making a new section I don't want to make new section when sure I am here o help you array comes.
CodePudding user response:
As I've seen your code, you're using String
to indicate the day of the message which is bad practice. Instead, you should use Date
, which will help you group the messages by date:
let sectionArray = messages.reduce(into: [section]()) { result, element in
if let section = result.firstIndex(where: {Calendar.current.isDate($0.date, inSameDayAs: element.date)}) {
result[section].append(element)//append message to section
}else {
//create new section
}
}
If you really need to use String
, use this:
let sectionArray = messages.reduce(into: [section]()) { result, element in
if let section = result.firstIndex(where: {$0.date == element.date}) {
result[section].append(element)//append message to section
}else {
//create new section
}
}