Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置TableView单元格的值时出现致命错误_Ios_Swift_Interface Builder - Fatal编程技术网

Ios 设置TableView单元格的值时出现致命错误

Ios 设置TableView单元格的值时出现致命错误,ios,swift,interface-builder,Ios,Swift,Interface Builder,我在尝试将值传递给文本标签时遇到此错误 Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: 在准备了这么多关于这个的帖子之后,我尝试了以下方法,但没有成功: 打印出photo中的每个值,该值是正确的,而不是nil 删除并重新连接插座 创建了一个伪值username1Label.text=“test”,并且出现了相同的错误 下面是代码的简化版本: class EventFeedCel

我在尝试将值传递给文本标签时遇到此错误

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value:
在准备了这么多关于这个的帖子之后,我尝试了以下方法,但没有成功:

  • 打印出
    photo
    中的每个值,该值是正确的,而不是
    nil
  • 删除并重新连接插座
  • 创建了一个伪值
    username1Label.text=“test”
    ,并且出现了相同的错误
  • 下面是代码的简化版本:

    class EventFeedCell: UITableViewCell {
    
    @IBOutlet weak var username1Label: UILabel!
    @IBOutlet weak var date1Label: UILabel!
    @IBOutlet weak var photoId1Label: UILabel!
    
    func setPhoto(_ photo:Photo) {
    
        username1Label.text = photo.byUsername  //I get the fatal error here
        date1Label.text = photo.date
        photoId1Label.text = photo.postId
    }}
    
    以及
    照片
    对象

    class Photo {
    
    var postId = ""
    var byId = ""
    var byUsername = ""
    var date = ""
    var url = ""
    var eventId = ""
    
    }

    cellForRowAt

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        //Get a event feed photo cell
        let cell = tableView.dequeueReusableCell(withIdentifier: EventFeedCell.eventFeedPhotoTableCell, for: indexPath) as! EventFeedCell
    
        //Get the photo for this row
        let photo = photos[indexPath.row]
    
        //Set the details for the cell
        cell.setPhoto(photo)
    
        return cell
    
    }
    

    我还能试什么?这是另一个工作的TableView单元格的精确副本(出于这个原因,我在标签名称中添加了一个
    1
    ,以确保它们是唯一的)

    一旦该单元格初始化,您的标签和其他UI组件可能为零,因此您必须仅在UI组件(标签等)出现时设置一个值已初始化,否则不设置。在访问文本属性之前,只需添加
    。 下面是您的
    setPhoto(:)
    方法的修改版本

    func setPhoto(_ photo:Photo) {
    
        username1Label?.text = photo.byUsername  
        date1Label?.text = photo.date
        photoId1Label?.text = photo.postId
    }
    
    
    需要注意的一点是,如果您的photoModel属性是可选的,则在尝试访问之前必须先将其展开。下面是
    setPhoto(:)
    方法的另一个选项,仅当您的photoModel具有可选属性时

    func setPhoto(_ photo:Photo) {
          // Optionally unwrap your photo properties.
         gaurd let byUserName = photo.byUsername,
          let  date = photo.date,
          let postId = photo.postId else {return}
    
        // Safe to proceed if all your values are set in photoModel
        username1Label?.text = byUserName  
        date1Label?.text = date
        photoId1Label?.text = postId
    }
    

    没有足够的详细信息,但您可能没有正确创建单元格,无法在运行时连接
    username1Label
    插座。请首先指出导致错误的确切行。另外,请向我们展示您的
    cellForRowAt
    代码,您可以在其中创建一个
    EventFeedCell
    单元格。请给我们一个建议,以避免将来的麻烦,永远不要使用“!”始终使用可选的?然后打开它们并处理它们为零的情况。问题可能是我在tableview中有一个tableview单元格在tableview中有一个tableview单元格吗?似乎连接正确暂时更改
    setPhoto(:)
    中的第一行以使用字符串文字,然后查看是否仍然有错误。如果是这样的话,问题就出在电池和代码之间的连接上,如果不是这样的话,问题就出在等式的照片端谢谢!这是基于添加``?
    的排序工作。在调用
    setPhoto```方法之前,是否有一种方法可以等待UI组件完全初始化?嗨,@akash23a我想你所要做的就是添加
    ,它在我的大多数代码库中都能正常工作,所以我想这就是你所需要的,但是如果你想挖掘更多,查看此处的文档并阅读有关UITableViewCell生命周期方法的文章。如果答案解决了您的问题,请不要忘记标记:)