如何将当前时间添加到 table 视图单元格 swift 中的标签 3
How to add current time to label in table view cell swift 3
我有一个添加按钮,可将您带到一个新视图,其中包含用于添加信息的文本输入。一旦你点击添加按钮,它就会带你回到表格视图并将所有输入添加到标签中。我无法将当前时间放入我制作的 dateStamp 标签中。有人可以帮忙吗?
主控制器
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)
}
}
}
为此,您可以使用委托!至少那是我学到的。
let timeStamp = "\(DateFormatter.localizedString(from: Date(), dateStyle: .long, timeStyle: .long))"
如果要将格式转换为截至今日
只需将日期传递给该函数,它就会 return 一个表示 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")
}
}
我有一个添加按钮,可将您带到一个新视图,其中包含用于添加信息的文本输入。一旦你点击添加按钮,它就会带你回到表格视图并将所有输入添加到标签中。我无法将当前时间放入我制作的 dateStamp 标签中。有人可以帮忙吗?
主控制器
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)
}
}
}
为此,您可以使用委托!至少那是我学到的。
let timeStamp = "\(DateFormatter.localizedString(from: Date(), dateStyle: .long, timeStyle: .long))"
如果要将格式转换为截至今日
只需将日期传递给该函数,它就会 return 一个表示 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")
}
}