Reputation: 39
So I have 3 cells: SenderCell
, ReceiverCell
, and a default blank Cell
I want the mainTableView to show all messages from both sender and receiver, which are contained in messageFromCurrentUserArray
and messageFromReceiverArray
respectively. Unfortunately, whenever I run the code below, it only shows either cell. I would like to show both cells if their arrays aren't empty. Hoping someone could help me. Thanks in advance!
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
}
}
Upvotes: 0
Views: 67
Reputation: 10199
The problem is the comparsion in the outer if statements:
if indexPath.row < messageFromCurrentUserArray.count {
//...
} else if indexPath.row < messageFromReceiverArray.count {
// ...
} else {
// ...
}
Since all the count
s in the arrays start with index 0
and stop at their count each, but your indexPath.row will go up to the the sum of both (e.g. messageFromCurrentUserArray.count + messageFromReceiverArray.count
)
You'll have to change the second if
clause and the index access to something like
} 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 {
// ...
}
Upvotes: 1