如何在表格视图单元格swift 3中将当前时间添加到标签

如何在表格视图单元格swift 3中将当前时间添加到标签,swift,uitableview,date,cell,datestamp,Swift,Uitableview,Date,Cell,Datestamp,我有一个“添加”按钮,可以将您带到一个新视图,其中包含文本输入以添加信息。点击add按钮后,它会将您带回tableview并将所有输入添加到标签。我无法将当前时间拉入我制作的邮戳标签。有人能帮忙吗 主控制器 var dateStamp = Date() var clientName = [""] var projecDescript = [""] // Custom cell to make all input fields custom class CustomCell: UITabl

我有一个“添加”按钮,可以将您带到一个新视图,其中包含文本输入以添加信息。点击add按钮后,它会将您带回tableview并将所有输入添加到标签。我无法将当前时间拉入我制作的邮戳标签。有人能帮忙吗

主控制器

 var dateStamp = Date()
 var clientName = [""]
 var projecDescript = [""]

// Custom cell to make all input fields custom
class CustomCell: UITableViewCell {
    //Make your outlets here, connect the outlets from cell in your storyboard

    @IBOutlet var clientNameLabel: UILabel!
    @IBOutlet var descriptionLabel: UILabel!
    @IBOutlet var dateStamp: UILabel!

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var clientTableList: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (clientName.count)
        return (projecDescript.count)
    }

    // This is the new items added into the inputs
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "clientCell", for: indexPath) as! CustomCell

        // Adds Clients Name
        let companyName = clientName[indexPath.row]
        cell.clientNameLabel?.text = companyName

        // Adds Clients Description
        let descriptionName = projecDescript[indexPath.row]
        cell.descriptionLabel?.text = descriptionName



        return cell
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func viewDidAppear(_ animated: Bool) {
        clientTableList.reloadData()
    }
import UIKit

class AddInvoice: UIViewController {

    @IBOutlet var clientNameInput: UITextField!
    @IBOutlet var descriptionNameInput: UITextView!



    @IBAction func addInvoice(_ sender: Any) {

        if clientNameInput.text != "" && descriptionNameInput.text != "" 
        {
            clientName.append(clientNameInput.text!)
            //clientInput.text = ""

            projecDescript.append(descriptionNameInput.text!)
            //descriptionFieldInput.text = ""


            _ = navigationController?.popViewController(animated: true)
        }  
    }
}
第二控制器

 var dateStamp = Date()
 var clientName = [""]
 var projecDescript = [""]

// Custom cell to make all input fields custom
class CustomCell: UITableViewCell {
    //Make your outlets here, connect the outlets from cell in your storyboard

    @IBOutlet var clientNameLabel: UILabel!
    @IBOutlet var descriptionLabel: UILabel!
    @IBOutlet var dateStamp: UILabel!

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var clientTableList: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (clientName.count)
        return (projecDescript.count)
    }

    // This is the new items added into the inputs
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "clientCell", for: indexPath) as! CustomCell

        // Adds Clients Name
        let companyName = clientName[indexPath.row]
        cell.clientNameLabel?.text = companyName

        // Adds Clients Description
        let descriptionName = projecDescript[indexPath.row]
        cell.descriptionLabel?.text = descriptionName



        return cell
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func viewDidAppear(_ animated: Bool) {
        clientTableList.reloadData()
    }
import UIKit

class AddInvoice: UIViewController {

    @IBOutlet var clientNameInput: UITextField!
    @IBOutlet var descriptionNameInput: UITextView!



    @IBAction func addInvoice(_ sender: Any) {

        if clientNameInput.text != "" && descriptionNameInput.text != "" 
        {
            clientName.append(clientNameInput.text!)
            //clientInput.text = ""

            projecDescript.append(descriptionNameInput.text!)
            //descriptionFieldInput.text = ""


            _ = navigationController?.popViewController(animated: true)
        }  
    }
}

为了做到这一点,您可以使用代理!至少这是我学到的


要做到这一点,您可以使用代理!至少这是我学到的

如果要将格式转换为截至今天的格式

只需将日期传递给该函数,它将返回一个字符串,如3周前

func relativePast(for date : Date) -> String {

    let units = Set<Calendar.Component>([.year, .month, .day, .hour, .minute, .second, .weekOfYear])
    let components = Calendar.current.dateComponents(units, from: date, to: Date())

    if components.year! > 0 {
        return "\(components.year!) " + (components.year! > 1 ? "years ago" : "year ago")

    } else if components.month! > 0 {
        return "\(components.month!) " + (components.month! > 1 ? "months ago" : "month ago")

    } else if components.weekOfYear! > 0 {
        return "\(components.weekOfYear!) " + (components.weekOfYear! > 1 ? "weeks ago" : "week ago")

    } else if (components.day! > 0) {
        return (components.day! > 1 ? "\(components.day!) days ago" : "Yesterday")

    } else if components.hour! > 0 {
        return "\(components.hour!) " + (components.hour! > 1 ? "hours ago" : "hour ago")

    } else if components.minute! > 0 {
        return "\(components.minute!) " + (components.minute! > 1 ? "minutes ago" : "minute ago")

    } else {
        return "\(components.second!) " + (components.second! > 1 ? "seconds ago" : "second ago")
    }
}
func relativePost(用于日期:date)->字符串{
让单位=设置([.年、.月、.日、.小时、.分钟、.秒、.周、.年])
让components=Calendar.current.dateComponents(单位,从:日期,到:日期())
如果组件数为0.1年!>0{
返回“\(components.year!)”+(components.year!>1?“年前”:“年前”)
}否则,如果components.month!>0{
返回“\(components.month!)”+(components.month!>1?“月前”:“月前”)
}否则,如果components.weekOfYear!>0{
返回“\(components.weekOfYear!)”+(components.weekOfYear!>1?“周前”:“周前”)
}否则如果(components.day!>0){
返回(components.day!>1?”\(components.day!)天之前:“昨天”)
}否则,如果components.hour!>0{
返回“\(components.hour!)”+(components.hour!>1?“小时前”:“小时前”)
}否则,如果components.minute!>0{
返回“\(components.minute!)”+(components.minute!>1?“分钟前”:“分钟前”)
}否则{
返回“\(components.second!)”+(components.second!>1?“秒前”:“秒前”)
}
}
如果要将格式转换为截至今天的格式

只需将日期传递给该函数,它将返回一个字符串,如3周前

func relativePast(for date : Date) -> String {

    let units = Set<Calendar.Component>([.year, .month, .day, .hour, .minute, .second, .weekOfYear])
    let components = Calendar.current.dateComponents(units, from: date, to: Date())

    if components.year! > 0 {
        return "\(components.year!) " + (components.year! > 1 ? "years ago" : "year ago")

    } else if components.month! > 0 {
        return "\(components.month!) " + (components.month! > 1 ? "months ago" : "month ago")

    } else if components.weekOfYear! > 0 {
        return "\(components.weekOfYear!) " + (components.weekOfYear! > 1 ? "weeks ago" : "week ago")

    } else if (components.day! > 0) {
        return (components.day! > 1 ? "\(components.day!) days ago" : "Yesterday")

    } else if components.hour! > 0 {
        return "\(components.hour!) " + (components.hour! > 1 ? "hours ago" : "hour ago")

    } else if components.minute! > 0 {
        return "\(components.minute!) " + (components.minute! > 1 ? "minutes ago" : "minute ago")

    } else {
        return "\(components.second!) " + (components.second! > 1 ? "seconds ago" : "second ago")
    }
}
func relativePost(用于日期:date)->字符串{
让单位=设置([.年、.月、.日、.小时、.分钟、.秒、.周、.年])
让components=Calendar.current.dateComponents(单位,从:日期,到:日期())
如果组件数为0.1年!>0{
返回“\(components.year!)”+(components.year!>1?“年前”:“年前”)
}否则,如果components.month!>0{
返回“\(components.month!)”+(components.month!>1?“月前”:“月前”)
}否则,如果components.weekOfYear!>0{
返回“\(components.weekOfYear!)”+(components.weekOfYear!>1?“周前”:“周前”)
}否则如果(components.day!>0){
返回(components.day!>1?”\(components.day!)天之前:“昨天”)
}否则,如果components.hour!>0{
返回“\(components.hour!)”+(components.hour!>1?“小时前”:“小时前”)
}否则,如果components.minute!>0{
返回“\(components.minute!)”+(components.minute!>1?“分钟前”:“分钟前”)
}否则{
返回“\(components.second!)”+(components.second!>1?“秒前”:“秒前”)
}
}

不,我明白了。它添加了一个时间戳,我正在努力解决。嗯,你在获取时间戳方面有困难吗?或者您有时间戳,但不知道如何将其添加到标签中?如果是第二种情况,那么正如视频中所提到的,当用户点击添加按钮时,您会得到当前的时间戳并将其发送回,然后将其添加到labelno,我想我首先要努力获得时间戳。我在网上看过好几件事,我尝试的每件事似乎都不起作用。我制作了一个var dateStamp=Date()。现在当我点击“添加发票”按钮时,我正在尝试获取该部分。它添加了一个时间戳,我正在努力解决。嗯,你在获取时间戳方面有困难吗?或者您有时间戳,但不知道如何将其添加到标签中?如果是第二种情况,那么正如视频中所提到的,当用户点击添加按钮时,您会得到当前的时间戳并将其发送回,然后将其添加到labelno,我想我首先要努力获得时间戳。我在网上看过好几件事,我尝试的每件事似乎都不起作用。我制作了一个var dateStamp=Date()。现在我正试图得到它,当我点击添加发票按钮谢谢你这是我一直在寻找的!我要到15岁才能投票,对此我很抱歉!有没有一个简单的方法让这个说。。。1分钟前。一天前。2周前,等等。或者我需要为此构建一个函数吗?太棒了!这是有道理的,而且比我要做的更容易!谢谢,这就是我要找的!我要到15岁才能投票,对此我很抱歉!有没有一个简单的方法让这个说。。。1分钟前。一天前。2周前,等等。或者我需要为此构建一个函数吗?太棒了!这是有道理的,而且比我要做的更容易!