ios - 如何在 swift ios 中对齐表格 View 单元格中的文本

标签 ios swift firebase

我正在使用 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/

相关文章:

ios - 应用程序中的两个选项卡 Controller

ios - 缩进 UITextField 文本和占位符

java - 为什么我们在向firestore发送数据时,将HashMap中的参数设置为object?

ios - 发送了无法识别的选择器

iphone - UITableView 搜索后应用程序崩溃

iOS 将捕获的图像保存到内存并访问它们

ios - 如何通过定时器索引数组并绑定(bind)到 View ?

android - 如何在 Cloud Firestore 中使用文档 ID 执行集合组查询

android - 应用内消息错误

ios - 如何传入纬度和经度参数 Yelp API