您好我正在尝试使用没有GitHub库的自定义聊天气泡。
我有5个swift文件。
自定义UILabel
MyBubble为UITableViewCell + xib
SomeoneBubble为UITableViewCell + xib
TableViewController。
这是我的UILabel.swift
import UIKit
@IBDesignable class ChattingBubble: UILabel {
@IBInspectable var topInset: CGFloat = 10.0
@IBInspectable var bottomInset: CGFloat = 10.0
@IBInspectable var leftInset: CGFloat = 16.0
@IBInspectable var rightInset: CGFloat = 16.0
@IBInspectable var radius: CGFloat = 10
@IBInspectable var borderWidth: CGFloat = 0
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.cgColor
}
}
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
self.layer.borderWidth = borderWidth
}
override var intrinsicContentSize : CGSize {
var intrinsicSuperViewContentSize = super.intrinsicContentSize
intrinsicSuperViewContentSize.height += topInset + bottomInset
intrinsicSuperViewContentSize.width += leftInset + rightInset
return intrinsicSuperViewContentSize
}
}
这是myBubble:UITableViewCell
class MyBubbleCell: UITableViewCell {
@IBOutlet weak var bubbleLbl: ChattingBubble!
@IBOutlet weak var eventLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.backgroundColor = UIColor(red: 242.0 / 255.0, green: 242.0 / 255.0, blue: 242.0 / 255.0, alpha: 1)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我认为看起来不错但我的问题是UITableViewController
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//define cell
let myBubble = tableView.dequeueReusableCell(withIdentifier: "MyBubbleCell", for: indexPath) as! MyBubbleCell
let someoneBubble = tableView.dequeueReusableCell(withIdentifier: "SomeoneBubbleCell", for: indexPath) as! SomeoneBubbleCell
let object = results[indexPath.row]
//My Bubble
if object.isMyBubble == true {
//LastCell will be shown "Time Label"
if indexPath.row != (results.count - 1){
myBubble.eventLbl.isHidden = true
myBubble.bubbleLbl.text = object.messageTxt
}else {
myBubble.bubbleLbl.text = object.messageTxt
//This code means waiting for notification from server. It just shown when enter the message... immediately
if object.messageId == nil {
myBubble.eventLbl.isHidden = true
myBubble.bubbleLbl.text = object.messageTxt
myBubble.bubbleLbl.textColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.5)
//When received from server then shown the text
}else {
myBubble.eventLbl.isHidden = false
myBubble.bubbleLbl.text = object.messageTxt
UIView.transition(with: myBubble.bubbleLbl, duration: 0.6, options: .transitionCrossDissolve, animations: { myBubble.bubbleLbl.textColor = .black }, completion: nil)
myBubble.eventLbl.text = object.msgDate
}
}
return myBubble
//Someone Bubble [I will update this code but It exactly same myBubble snippet]
}else {
return someoneBubble
}
}
我正在使用parse-server sdk。我的逻辑有两个UITableViewCell因为有不同的位置。一个是右侧,另一个是左侧。
所以tableView的功能应该检查我的泡泡是否。
它会影响滚动性能。
此外,我的代码有很多if语句来显示从currentUser输入的消息。
因为如果没有立即显示消息,那么用户必须等待响应,直到消息被保存到服务器中。
有人建议我吗?