0

I have a table cell view with, currently a date and title. But I want to add the current time to it as well, but I cant seem to add another section in the cell. Can someone show me how?

Here is my cellForRowAt:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath)

    let contact = userInput[indexPath.row]

    cell.detailTextLabel?.numberOfLines = 3
    cell.detailTextLabel?.text = contact.value(forKey:"thf") as? String
    cell.textLabel?.text = contact.value(forKey:"date") as? String

    return cell
}

I dont mind adding the time anywhere but preferably somewhere noticeable

Here is how it looks like

Dadummn
  • 31
  • 4
  • Possible duplicate of [Custom UITableViewCell from nib in Swift](https://stackoverflow.com/questions/25541786/custom-uitableviewcell-from-nib-in-swift) – RLoniello Nov 01 '18 at 03:14
  • What you need is a custom table view cell, you can add your own UI elements to the view of the cell. There are plenty of resources once you know what you are looking for a quick google search for "custom uitableviewcell swift " will yield many useful results. – RLoniello Nov 01 '18 at 03:15
  • Why not put both the date and time in the text label? No need to customize the cell if that's good enough. – rmaddy Nov 01 '18 at 03:33

1 Answers1

0
  1. First of all write the below code into your date Extension

    extension Date {
        func getFormattedDate(format: String) -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = format
            let strDate = dateFormatter.string(from: self)
            return strDate
        }
    
        func getTimeStamp() -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "hh:mm a"
            let dateString = dateFormatter.string(from: self)
            return dateString
        }
    }
    
  2. Now use date methods as I have used below:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath)
        let contact = userInput[indexPath.row]
        cell.yourDateLabel?.text = (contact.value(forKey:"keyforDate") as? Date).getFormattedDate(format: "MMM dd,yyyy")
        cell.yourTimeLabel?.text = (contact.value(forKey:"keyforDate") as? Date).getTimeStamp()
        return cell    
    }
    
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ahemadabbas Vagh
  • 492
  • 3
  • 15