【问题标题】:fatal error: unexpectedly found nil while unwrapping an Optional value (cellForRowAtIndexPath) (Xib Custom Cell Swift)致命错误:在展开可选值时意外发现 nil (cellForRowAtIndexPath) (Xib Custom Cell Swift)
【发布时间】:2015-07-05 15:08:14
【问题描述】:

由于我引用的单元格(在 DownloadProfilePicture 中)可见,我收到主题中所述的错误,原因不明。基本上,我正在尝试复制/调整所做的事情 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}

【问题讨论】:

  • let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
  • 我已经尝试了你的建议。不幸的是,它没有改变任何东西。
  • FWIW Cocoa* 约定是变量和方法名以小写字母开头,类名以大写字母开头。当我看到:topCell 我最初认为这是一个变量。约定确实有帮助。
  • 感谢您的评论。
  • 看起来 indexPath 上没有单元格,您正在明确解开它。如果该单元格不可见,则它可能不存在。永远不要明确解开任何可能在任何情况下都为零的东西。来自文档:表示表格单元格的对象,如果单元格 不可见indexPath 超出范围,则为 nil。看得见吗?

标签: ios swift uitableview xib custom-cell


【解决方案1】:

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

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
}

【讨论】:

  • 谢谢。我只是没有包含完整的方法,因为它有点长。你可以在这里查看pastebin.com/q7rLbLq9
  • 确保单元在每个分支中只实例化一次。还有两个地方。删除第一个(全局)。并避免在其他方法中显式调用cellForRowAtIndexPath。更新模型并重新加载表格视图
  • 好的!但是为什么苹果在其他方法中调用 cellForRowAtIndexPath 呢?来自“LazyTableViewImages”:pastebin.com/pV1C0Uvg
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多