0

所以我在使用套接字 io 中创建了一个聊天页面,我有 2 个用于传入和传出消息的标签。

我有一个名为 ChatText 的变量,它将像这样存储内容:

[[“嗨,你好吗?”,“0”],[“我很好,你呢?”,“1”]]

其中 0 = 已发送,1 = 已接收

所以我可以知道哪个是发送和接收的消息,并将它们设置为标签并设置它们的样式

我不知道这是否是正确的方法,我在互联网上搜索找不到太多信息,所以我就这样做了,请告诉我是否错误或告诉我如何解决这个问题。

这是我到目前为止的代码,我认为问题一定是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell = tableView.dequeueReusableCellWithIdentifier("chatCell", forIndexPath: indexPath) as? TableChatCell


    cell!.selectionStyle = UITableViewCellSelectionStyle.None


    let ChatTextFixed = self.ChatText[indexPath.row] as NSArray
    if ChatTextFixed.count > 0 {
        if ChatTextFixed[1] as! Int == 0 {
            cell!.ChatLableS.text = ChatTextFixed[0] as? String
            cell!.RView.hidden = true
        }

        if ChatTextFixed[1] as! Int == 1  {
            cell!.ChatLableR.text = ChatTextFixed[0] as? String
            cell!.SView.hidden = true
        }
        print(ChatTextFixed)
    }

    cell!.ChatLableS.textColor = UIColor.whiteColor()
    cell!.ChatLableR.textColor = UIColor.blackColor()

    return cell!
}

注意:如果我删除这些 If 条件:

if ChatTextFixed[1] as! Int == 0 {}
if ChatTextFixed[1] as! Int == ! {}

并将文本设置为 1 标签,它的工作原理如下:

cell!.ChatLableS.text = ChatTextFixed[0] as? String
cell!.RView.hidden = true

更新

问题是因为

cell!.RView.hidden = true
cell!.SView.hidden = true

但我怎样才能隐藏另一个标签并只显示一个!

4

2 回答 2

0

在此处输入图像描述

    let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! MyTableViewCell

    let ChatTextFixed = ChatText[indexPath.row]
    if let index = Int(ChatTextFixed[1]) where index == 0 {
        cell.ChatLableS.text = ChatTextFixed[0]
        cell.ChatLableR.hidden = true
        cell.RView.hidden = true
    } else {
        cell.ChatLableR.text = ChatTextFixed[0]
        cell.ChatLableS.hidden = true
        cell.SView.hidden = true
    }

    cell.ChatLableS.textColor = UIColor.whiteColor()
    cell.ChatLableR.textColor = UIColor.blackColor()

    return cell
于 2016-10-31T00:53:33.833 回答
0

我不知道为什么,但是像这样更改我的代码是可行的:

       if ChatTextFixed[1] as! Int == 0 {
            cell!.ChatLableS.text = ChatTextFixed[0] as? String
            cell!.ChatLableR.text = ""
            cell!.RView.backgroundColor = UIColor.whiteColor()
            cell!.SView.backgroundColor = UIColor(red: 69/225, green: 189/225, blue: 120/225, alpha: 1.0)
            //cell!.RView.hidden = true
        }

        if ChatTextFixed[1] as! Int == 1  {
            cell!.ChatLableR.text = ChatTextFixed[0] as? String
            cell!.ChatLableS.text = ""
            cell!.SView.backgroundColor = UIColor.whiteColor()
            cell!.RView.backgroundColor = UIColor(red: 200/225, green: 200/225, blue: 200/225, alpha:1.0)
            //cell!.SView.hidden = true
        }

所以我将文本设置为一个标签并将“”设置为花药标签,然后将其隐藏,这很有效,但我不知道为什么!只需要玩,直到它像这样工作!

于 2016-11-01T01:40:09.567 回答