【问题标题】:How to align text in tableview cell in swift ios如何在swift ios中对齐tableview单元格中的文本
【发布时间】:2018-08-15 12:28:57
【问题描述】:

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

【问题讨论】:

  • 制作两个细胞,一个是发送者细胞,一个是接收者细胞。 if object confrorms to sender deque sender celll 否则为接收者 cell。
  • 你能提供更详细的代码吗?

标签: ios swift firebase


【解决方案1】:

在情节提要中设置约束(尾随和前导),然后在您的自定义单元格类中创建它们的@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
}

【讨论】:

  • 我尝试了这段代码,但它要么将所有消息向右或向左移动。
  • @farooqmughal 您是否尝试过这个而不是 if uuid == senderid 语句中的代码?
  • 是的,我试过了。如果 uuid== senderid 那么它将所有单元格向右移动
  • 你确定 uuid 可以是 != senderid 吗?
  • 是的。当我将消息保存到火力基地时。我用 uuid 保存。在检索时,我首先检查 uuid=senderid 是否正确
猜你喜欢
  • 1970-01-01
  • 2010-12-14
  • 2014-12-30
  • 2010-11-03
  • 1970-01-01
  • 2012-11-07
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多