ios - 为什么我不能在数组中附加标签?

标签 ios swift

我从变量中的 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)

 }
}

Here is the error

最佳答案

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/

相关文章:

ios - 设置 WatchKit 表

regex - 用于从对话中删除所有日期时间的正则表达式

iOS:CHCSVParser 和 NSPredicate?

iphone - 离线 gps 坐标访问 - iPhone/Android

swift - 根据选择显示正确的文本颜色?

xcode - 只为 UICollectionViewFlowLayout 缓存不匹配的帧记录一次

ios - iOS中如何根据不同用户类型动态切换UI主题

ios - Doxygen/headerdoc 是否支持 swift 4.0?

javascript - 在 IOS5 之前,我可以使用 Javascript 使 UIWebView 中的 PDF 滚动。在IOS5下如何做到这一点?

iphone - iAds : How can I prevent my AdBannerView delegate from getting called after the view has been released?