我从变量中的 datePicker
获取 dateChanged 的值,然后将该变量附加到我的字符串数组中。然后我将该 StringArray 打印到我的 TableView 单元格中,但我无法弄清楚为什么它不起作用。这是我将时间声明为全局变量的代码:
var time: [String] = []
func datePickerValueChanged(sender:UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.short
dateFormatter.timeStyle = DateFormatter.Style.short
let mydate="adding reminder on \(dateFormatter.string(from: sender.date))"
time.append(mydate)
}
然后我尝试通过代码打印我的 TableViewCell 中的值:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellID") as! CustomCell
cell.dateLabel?.text=time[indexPath.row]
return (cell)
这是我的 CustomCell 类的代码:
class CustomCell: UITableViewCell {
@IBOutlet weak var dateLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
最佳答案
在numberOfRowsInSection
方法中返回list.count
并且在 cellForRowAt indexPath
cell.dateLabel?.text = time.indices.contains(indexPath.row) ? time[indexPath.row] : "Not Available"
关于ios - 为什么我不能在数组中附加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42824933/