我正在开发一个使用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
}发布于 2018-08-15 20:43:31
在故事板中设置约束(尾随和前导),然后在您的自定义单元格类中创建@IBOutlet,检查senderid == uid和更改约束。例如,左1为8,右1为120,表示当前用户的消息;左1为120,右1为8。
如果您的代码对您有好处,那么只需:
cell?.textLabel?.text = postData[indexPath.row]
if uuid == senderid {
cell?.textLabel?.textAlignment = .right
} else {
cell?.textLabel?.textAlignment = .left
}https://stackoverflow.com/questions/51858710
复制相似问题