0
所以,我有3個小區:SenderCell
,ReceiverCell
和默認的空白Cell
UITableView的僅示出了1個細胞時,它應該顯示2個細胞(SWIFT 3)
我想要的mainTableView顯示來自發送者和接收者的所有消息,分別載於messageFromCurrentUserArray
和messageFromReceiverArray
。不幸的是,每當我運行下面的代碼時,它只顯示任一單元格。我想顯示兩個單元格,如果他們的數組不是空的。希望有人能幫助我。提前致謝!
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
}
}
它的工作!非常感謝! – acoustickat