fatal error: unexpectedly found nil while unwrapping an Optional value (cellForRowAtIndexPath) (Xib Custom Cell Swift)

fatal error: unexpectedly found nil while unwrapping an Optional value (cellForRowAtIndexPath) (Xib Custom Cell Swift)

由于我引用的单元格(在 DownloadProfilePicture 中)可见,我收到主题中所述的错误,原因不明。基本上,我正在尝试 replicate/adapt 在 LazyTableViewImages

override func viewDidLoad() {
         super.viewDidLoad()
         self.tableView.registerClass(topCell.self, forCellReuseIdentifier: "topCell")
         self.tableView.registerClass(contentCell.self, forCellReuseIdentifier: "contentCell")
         tableView.registerNib(UINib(nibName: "topCell", bundle: nil), forCellReuseIdentifier: "topCell")
         tableView.registerNib(UINib(nibName: "contentCell", bundle: nil), forCellReuseIdentifier: "contentCell")
         self.tableView.delegate = self
         self.tableView.dataSource = self
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let nodeCount = arrayOfPosts.count;
    let cell = UITableViewCell()
     tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
        if (nodeCount > 0)
        {
            if (indexPath.row % 2 == 0){
                // Set up the cell representing the app
                var cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
                let post = arrayOfPosts[indexPath.row]
                cell.userName.text = post.userName
                cell.timeSincePosted.text = post.timeSincePosted
                // Only load cached images; defer new downloads until scrolling ends
                if (post.profileImage == nil)
                {
                    if (!tableView.dragging && !tableView.decelerating)
                    {
                        downloadProfileImage(post, indexPath: indexPath)
                        return cell
                    }
                    // if a download is deferred or in progress, return a placeholder image
                    cell.profileImage.image = UIImage(named: "titanic.jpg")
                                       }
                else
                {
                       cell.profileImage.image = post.profileImage
                }
                return cell
            }

func downloadProfileImage(post: Post, indexPath: NSIndexPath){
        println(self.tableView.indexPathsForVisibleRows())
        println(indexPath)
        let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath) as! topCell
        cell.profileImage.image = UIImage(named: "img1")
    }

可见路径:

 (
    "<NSIndexPath: 0x7888f7b0> {length = 2, path = 0 - 0}",
    "<NSIndexPath: 0x788a4d60> {length = 2, path = 0 - 1}"
)

单元格:

<NSIndexPath: 0x788a3c80> {length = 2, path = 0 - 1}

在该方法中,table 视图单元格被创建了两次。 试试这样的结构,必须保证返回一个table视图单元格

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let nodeCount = arrayOfPosts.count;
  let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
  if (nodeCount > 0)  {
    if (indexPath.row % 2 == 0){
      let post = arrayOfPosts[indexPath.row]
      cell.userName.text = post.userName
      cell.timeSincePosted.text = post.timeSincePosted
      // Only load cached images; defer new downloads until scrolling ends
      if (post.profileImage == nil)
      {
        if (!tableView.dragging && !tableView.decelerating)
        {
          downloadProfileImage(post, indexPath: indexPath)
          return cell
        }
        // if a download is deferred or in progress, return a placeholder image
        cell.profileImage.image = UIImage(named: "titanic.jpg")
      }
      else
      {
        cell.profileImage.image = post.profileImage
      }
    }
  }
  return cell
}