iOS:刷新tableview

时间:2017-01-11 11:42:22

标签: ios swift xcode uitableview

我正在为tableview页面创建一个pull-to-refresh功能。刷新步骤如下:

  1. 调用API并从互联网上获取数据
  2. 使用数据创建另一个电话
  3. 使用tableView.reloadData()
  4. 使用步骤2中的数据重新加载tableview上的数据

    当拉到刷新时,它有时会起作用,但有时会在控制台中出现错误消息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.subjectLblcell.titleLblcell.dateLblcell.courseImgFeedCell

    中的出口

0 个答案:

没有答案