我正在为tableview页面创建一个pull-to-refresh功能。刷新步骤如下:
tableView.reloadData()
当拉到刷新时,它有时会起作用,但有时会在控制台中出现错误消息fatal error: Index out of range
而导致EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)
消息标记在if
代码块上。我不知道错误是什么。
这是错误显示的功能
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if feedList[indexPath.row].type == "announcement" {
let cell = feedTable.dequeueReusableCell(withIdentifier: "announcementCell") as! FeedCell
cell.subjectLbl.text = "Announcement - "+feedList[indexPath.row].courseName
cell.titleLbl.text = feedList[indexPath.row].title
cell.dateLbl.text = "Posted on "+feedList[indexPath.row].date
cell.courseImg.sd_setImage(with: feedList[indexPath.row].imgSrc)
return cell
}
else if feedList[indexPath.row].type == "assignment" {
let cell = feedTable.dequeueReusableCell(withIdentifier: "assignmentCell") as! FeedCell
cell.subjectLbl.text = "Assignment - "+feedList[indexPath.row].courseName
cell.titleLbl.text = feedList[indexPath.row].title
cell.dateLbl.text = "Due date "+feedList[indexPath.row].dueDate!
cell.courseImg.sd_setImage(with: feedList[indexPath.row].imgSrc)
return cell
}
else {
let cell = feedTable.dequeueReusableCell(withIdentifier: "materialCell") as! FeedCell
cell.subjectLbl.text = "Material - "+feedList[indexPath.row].courseName
cell.titleLbl.text = feedList[indexPath.row].title
cell.dateLbl.text = "Posted on "+feedList[indexPath.row].date
cell.courseImg.sd_setImage(with: feedList[indexPath.row].imgSrc)
return cell
}
}
注意:
已安装 SDWebImage
库
FeedCell
是继承自UITableViewCell
feedList
结构与此[(type:String, courseName:String, imgSrc:URL, title:String, date:String, dueDate:String?)]
cell.subjectLbl
,cell.titleLbl
,cell.dateLbl
和cell.courseImg
是FeedCell