【问题标题】:UITableView only shows 1 cell when it should show 2 cells (Swift 3)UITableView 应该显示 2 个单元格时只显示 1 个单元格(Swift 3)
【发布时间】:2017-10-19 09:59:28
【问题描述】:

所以我有 3 个单元格:SenderCellReceiverCell 和默认空白 Cell

我希望 mainTableView 显示来自发送者和接收者的所有消息,它们分别包含在 messageFromCurrentUserArraymessageFromReceiverArray 中。不幸的是,每当我运行下面的代码时,它只显示任一单元格。如果它们的数组不为空,我想显示两个单元格。希望有人可以帮助我。提前致谢!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return allMessages.count

}

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


    if indexPath.row < messageFromCurrentUserArray.count {

        let cell = mainTableView.dequeueReusableCell(withIdentifier: "SenderCell", for: indexPath) as! SenderTableViewCell

        cell.senderMessage.text = messageFromCurrentUserArray[indexPath.row]

        mainTableView.beginUpdates()
        cell.senderMessage.sizeToFit()
        let imageFile = PFUser.current()?["profilePhoto"] as! PFFile
        imageFile.getDataInBackground(block: { (data, error) in

            if let imageData = data {

                self.currentUserImage = UIImage(data: imageData)!

            }

        })

        cell.senderProfilePhoto.image = currentUserImage
        cell.senderProfilePhoto.layer.masksToBounds = false
        cell.senderProfilePhoto.layer.cornerRadius = cell.senderProfilePhoto.frame.height / 2
        cell.senderProfilePhoto.clipsToBounds = true
        mainTableView.endUpdates()

        return cell

    } else if indexPath.row < messageFromReceiverArray.count {

        let cell = mainTableView.dequeueReusableCell(withIdentifier: "ReceiverCell", for: indexPath) as! ReceiverTableViewCell
        cell.receiverMessage.text = messageFromReceiverArray[indexPath.row]

        mainTableView.beginUpdates()
        cell.receiverMessage.sizeToFit()
        cell.receiverProfilePhoto.image = receiverImage
        cell.receiverProfilePhoto.layer.masksToBounds = false
        cell.receiverProfilePhoto.layer.cornerRadius = cell.receiverProfilePhoto.frame.height / 2
        cell.receiverProfilePhoto.clipsToBounds = true
        mainTableView.endUpdates()

        return cell

    } else {

        let cell = mainTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellTableViewCell
        return cell
    }

}

【问题讨论】:

    标签: ios swift uitableview swift3


    【解决方案1】:

    问题在于外部 if 语句中的比较:

    if indexPath.row < messageFromCurrentUserArray.count {
        //...
    } else if indexPath.row < messageFromReceiverArray.count {
        // ...
    } else {
        // ...
    }
    

    由于数组中的所有counts 都以索引0 开头并在每个计数处停止,但您的 indexPath.row 将上升到两者的总和(例如messageFromCurrentUserArray.count + messageFromReceiverArray.count

    您必须将第二个 if 子句和索引访问更改为类似

    } else if indexPath.row < (messageFromCurrentUserArray.count + messageFromReceiverArray.count) {
        // ...
        int receiverIndex = indexPath.row - messageFromCurrentUserArray.count
        cell.receiverMessage.text = messageFromReceiverArray[receiverIndex]
        // or: cell.receiverMessage.text = allMessages[indexPath.row]
        // ...
    }   else {
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多