我正在使用 firebase 在 ios swift 中开发一个聊天应用程序。我能够成功发送和检索消息。
但问题是整个消息要么向右要么向左显示。我想在右侧显示发送者消息,在左侧显示接收者消息。使用下面的代码,它可以向左或向右显示所有消息。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellchat:TVCChatTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellchat", for: indexPath) as! TVCChatTableViewCell
cellchat.setChat(chat: listofChatinfo[indexPath.row])
return cellchat
let cell = lachatlist.dequeueReusableCell(withIdentifier: "PostCell")
let uuid = Auth.auth().currentUser?.uid
if uuid == senderid {
cell?.textLabel?.textAlignment = .right
cell?.textLabel?.text = postData[indexPath.row]
}
// cell.setChat
//PostCell.
return cell!
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
最佳答案
在 Storyboard 中设置约束(尾随和前导),然后在自定义单元类中对其进行 @IBOutlets,检查 senderid == uid 是否并更改约束。例如当前用户的消息左一为8,右一为120,当前用户的消息左一为120,右一为8。
如果您的代码适合您,那么只需:
cell?.textLabel?.text = postData[indexPath.row]
if uuid == senderid {
cell?.textLabel?.textAlignment = .right
} else {
cell?.textLabel?.textAlignment = .left
}
关于ios - 如何在 swift ios 中对齐表格 View 单元格中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51858710/