繁体   English   中英

如何在Swift iOS的TableView单元格中对齐文本

[英]How to align text in tableview cell in swift ios

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

在情节提要中设置约束(跟踪和引导),然后在您的自定义单元格类中将它们设置为@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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM