我想为我的应用实现聊天气泡,除了这些单元格的定位外,一切都很顺利。我想知道是否有办法解决这个问题,拥有这样的屏幕真是太尴尬了 (此表视图中我只有1个部分和2行) 对于细胞而言,细胞看起来像这样
第二行应该在第二个单元格中,但它出现在第三个单元格中,我不知道为什么
这些是我的自定义类的自定义视图的代码:
class DialogCell: UITableViewCell {
@IBOutlet var iconImageView: UIImageView!
@IBOutlet var messageBackgroundView: UIImageView!
@IBOutlet var messageContentTextView: UITextView!
var maximumSize:CGSize = CGSize(width: 150, height: 1000)
var padding:CGFloat = 10
var exactSize:CGSize = CGSize(width: 0, height: 0)
let magicNumber:CGFloat = 50
func setViews(icon:UIImage,messageContent:String,backgroungImage:UIImage){
iconImageView = UIImageView()
messageContentTextView = UITextView()
messageBackgroundView = UIImageView()
iconImageView.layer.cornerRadius = magicNumber / 8
iconImageView.layer.masksToBounds = true
let orginX = self.frame.origin.x
let orginY = self.frame.origin.y
self.iconImageView.frame.origin.x = orginX + padding
self.iconImageView.frame.origin.y = orginY + padding
self.iconImageView.image = icon
self.iconImageView.frame.size = CGSize(width: magicNumber, height: magicNumber)
messageContentTextView.text = messageContent
exactSize = messageContentTextView.sizeThatFits(maximumSize)
self.messageContentTextView.frame = CGRect(origin: CGPoint(x: orginX + 3 * padding + magicNumber, y: orginY + padding),
size: exactSize)
self.messageContentTextView.allowsEditingTextAttributes = false
// self.messageContentTextView.
self.messageContentTextView.backgroundColor = UIColor.clearColor()
// exactSize.height
exactSize.width += 1.5 * padding
exactSize.height += 0.5 * padding
var new_image = backgroungImage.resizableImageWithCapInsets(UIEdgeInsets(top: 15, left: 10, bottom: 5, right: 5), resizingMode: UIImageResizingMode.Tile)
messageBackgroundView.image = new_image
self.messageBackgroundView.frame = CGRect(origin: CGPoint(x: orginX + 2 * padding + magicNumber, y: orginY + padding),
size: exactSize)
self.layer.opacity = 0.3
self.addSubview(iconImageView)
self.addSubview(messageBackgroundView)
self.addSubview(messageContentTextView)
}
![}][2]
我的tableView控制器中的主要方法
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as DialogCell
// dialogues[indexPath.row % dialogues.count]
var str:String = "\(indexPath.row) Line"
for _ in 0...2 {
str += str
}
cell.textLabel?.text = "\(indexPath.row) Line"
cell.setViews(UIImage(named: "minion")!, messageContent: str, backgroungImage: UIImage(named: "dialogue")!)
return cell}
答案 0 :(得分:0)
我不知道确切的答案,但我可以用不同的方式提出建议。
您可以将UIScrollView添加到控制器,每次收到消息时都会动态地将其添加到scrollView并更改scroll的contentSize。此外,您每次都需要控制标签位置。这可能是另一种方式,但它不是你的答案。