Добавить время, дату и заголовок в представление ячейки UITable - PullRequest
0 голосов
/ 01 ноября 2018

У меня есть вид ячейки таблицы, в настоящее время дата и заголовок. Но я хочу добавить текущее время, но я не могу добавить еще один раздел в ячейку. Может кто-нибудь показать мне, как?

Вот мой 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
}

Я не против добавить время в любом месте, но желательно где-нибудь заметное

Вот как это выглядит

1 Ответ

0 голосов
/ 01 ноября 2018
  1. Прежде всего напишите приведенный ниже код в вашу дату. Расширение

    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. Теперь используйте методы даты, которые я использовал ниже:

    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    
    }
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...