展开时,单元格引用为nil

时间:2017-09-23 03:09:57

标签: ios swift uitableview

我正在尝试使用两个不同的xib,并根据“发送者的消息”填充单元格。此确定似乎工作正常,但当我尝试设置单元格的属性时,我收到以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value
2017-09-22 23:03:59.135734-0400 Phlare[67547:4037478] fatal error: unexpectedly found nil while unwrapping an Optional value

错误出现在该行的else语句中:

cell.messageLabel.text = "Phlare"

这是发生错误的函数:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        //below is the creation of the nib variables
        let nib = UINib(nibName: "OutgoingUserMessage", bundle: nil)
        let nib2 = UINib(nibName: "IncomingUserMessage", bundle: nil)

        let msg = messageArray[indexPath.row]

        //below iterates through the array of sender ID's
        for i in 0...messageArray.count {
            print("for loop hitting")
            //the senderArray only holds an int if the user was the sender for that message

            //check to see if the sender is equal to the current user
            if(senderArray.contains(i)) {
                print("if statement hitting")
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! IncomingUserMessage
                //this means that the incoming message nib should be loaded
                self.myTableView.register(nib2, forCellReuseIdentifier: "cell")
                print("did not pick up sender")
                cell.messageLabel.text = "Phlare"
                return cell

            } else {
                print("the else loop is hitting")

                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! OutgoingUserMessage
                //this meand that the outgoing message nib should be loaded
                self.myTableView.register(nib, forCellReuseIdentifier: "cell")
                print("picked up sender")
                cell.messageLabel.text = "Phlare"
                return cell
            }
        }
        return UITableViewCell()
    }

有谁知道为什么会出现这个错误?

0 个答案:

没有答案