【发布时间】:2018-06-04 14:44:14
【问题描述】:
我有一个带有自定义单元格的 UITableView:
标题、标签、文本
这些是 UITextView 的 3 个元素,现在我的核心数据中有一个实体:
我想把标题、标题、标签中的日期和文本中的 introText:
我这样做了:
do{
var request = NSFetchRequest<NSFetchRequestResult>(entityName: "Actualites")
let count = try context.count(for: request)
if (count == 0) {
REST().SaveArticles(limit: 5, limitstart: 0, catid: 6, key: "xxx", context: context)
} else {
var actualites = [Actualites]()
actualites = try context.fetch(request) as! [Actualites]
let cell:ActuTblCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! ActuTblCell
for actualites in actualites {
print(actualites.title)
}
}
}catch{
print(error)
}
我可以在循环中获取标题,但是如何显示数据?我必须创建 3 个数组吗?
编辑: 我想我必须在这里做一个循环?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ActuTblCell
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy HH:mm"
cell.titleActuCell.text = actualites[indexPath.row].title
cell.dateActuCell.text = formatter.string(from: actualites[indexPath.row].created! as Date)
cell.descriptionActuCell.text = actualites[indexPath.row].introText
return cell
}
线程 1:致命错误:索引超出范围
【问题讨论】:
标签: swift uitableview core-data