gpt4 book ai didi

ios - "if"语句的问题 : "trying to style message. 内容根据 message.sender 有所不同

转载 作者:行者123 更新时间:2023-11-30 14:13:25 28 4
gpt4 key购买 nike

我创建了一类消息,包括内容和发件人。我成功地将所需的数据存储在 Parse 中,并且正在查询它们。到目前为止,没有问题。然后,我尝试根据发送者或接收者过滤消息,以便在我的 tableView 上以不同的方式显示它们。例如,假设发送者是 currentUser,则文本为蓝色,否则为绿色。如果 currentUser 发送一条消息,所有文本都会变成蓝色,即使是其他用户发送的文本;反之亦然。

class Message {

var sender = ""
var message = ""
var time = NSDate()

init(sender: String, message: String, time: NSDate)
{
self.sender = sender
self.message = message
self.time = time
}
}

var message1: Message = Message(sender: "", message: "", time: NSDate())

func styleTextView()
{
if message1.sender == PFUser.currentUser()?.username {
self.textView.textColor = UIColor.blueColor()
} else {
self.textView.textColor = UIColor.greenColor()
}
}


func addMessageToTextView(message1: Message)
{
textView.text = message1.message
self.styleTextView()
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: messageCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! messageCell

let message = self.array[indexPath.row]

cell.setupWithMessage(message)

return cell
}

我的 viewController 上还有其他代码,但我相信它们与此事无关;如果需要,我可以提供。知道为什么我不能根据发件人的不同风格使用不同的 TextView 吗?它们要么是蓝色,要么是绿色。

下面是 tableViewCell 完整设置的代码:

class messageCell: UITableViewCell {

private let padding: CGFloat = 10.0
var array1 = [Message]()

private func styleTextView()
{

let halfTextViewWidth = CGRectGetWidth(textView.bounds) / 2.0
let targetX = halfTextViewWidth + padding
let halfTextViewHeight = CGRectGetHeight(textView.bounds) / 2.0

self.textView.font = UIFont.systemFontOfSize(12.0)

if PFUser.currentUser()?.username == message1.sender && textView.text == message1.message {

self.textView.backgroundColor = UIColor.blueColor()
self.textView.layer.borderColor = UIColor.blueColor().CGColor
self.textView.textColor = UIColor.whiteColor()
textView.center.x = targetX
textView.center.y = halfTextViewHeight
} else {

self.textView.backgroundColor = UIColor.orangeColor()
self.textView.layer.borderColor = UIColor.orangeColor().CGColor
self.textView.textColor = UIColor.whiteColor()
self.textView.center.x = CGRectGetWidth(self.bounds) - targetX
self.textView.center.y = halfTextViewHeight
}
}


private lazy var textView: MessageBubbleTextView = {
let textView = MessageBubbleTextView(frame: CGRectZero, textContainer: nil)
self.contentView.addSubview(textView)
return textView
}()



class MessageBubbleTextView: UITextView
{
override init(frame: CGRect = CGRectZero, textContainer: NSTextContainer? = nil)
{
super.init(frame: frame, textContainer: textContainer)
self.scrollEnabled = false
self.editable = false
self.textContainerInset = UIEdgeInsets(top: 7, left: 7, bottom: 7, right: 7)
self.layer.cornerRadius = 15
self.layer.borderWidth = 2.0
}

required init(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
}

private let minimumHeight: CGFloat = 30.0
private var size = CGSizeZero

private var maxSize: CGSize {
get {
let maxWidth = CGRectGetWidth(self.bounds)
let maxHeight = CGFloat.max
return CGSize(width: maxWidth, height: maxHeight)
}
}


func setupWithMessage(message: Message) -> CGSize
{
textView.text = message.message
size = textView.sizeThatFits(maxSize)
if size.height < minimumHeight {
size.height = minimumHeight
}
textView.bounds.size = size
self.styleTextView()
return size
}
}

最佳答案

这一行可能存在一个可能的错误:

if message1.sender == PFUser.currentUser()?.username

您正在比较不同的类型,因为 message1 是一个 StringPFUser.currentUser()?.username 是一个可选字符串。比较它们的正确方法是:

if let username = PFUser.currentUser()?.username where username == message1.sender {
self.textView.textColor = UIColor.blueColor()
} else {
self.textView.textColor = UIColor.greenColor()
}

关于ios - "if"语句的问题 : "trying to style message. 内容根据 message.sender 有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500105/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com