【发布时间】:2016-09-07 17:47:23
【问题描述】:
我在 tableview 单元格中附加了一个 UItextView。现在我想像聊天气泡一样在它后面插入一个 UIimage。但是每次滚动 tableview 时,Uitextview 的宽度都会变小,高度会变长。文本也从单行调整为多行。此外,当我滚动回较早的单元格(例如第一行)时,也会发生同样的事情。 我无法弄清楚为什么会这样。
我的代码是: (SampleTableViewCell 是我的自定义 UItableViewCell 类,lblTitle 是我的 UitextView 出口。)
func tableView(tableView: UITableView, willDisplayCell cell: SampleTableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
cell.lblTitle.removeConstraints(cell.lblTitle.constraints)
let stringTitle = carName[indexPath.row] as String //carName is an array
cell.lblTitle.text=stringTitle
cell.lblTitle.sizeToFit()
let h = cell.lblTitle.frame.size.height
let w = cell.lblTitle.frame.size.width
let constraints = NSLayoutConstraint(item: cell.lblTitle, attribute: .Width, relatedBy: .Equal, toItem: cell.lblTitle, attribute: .Height, multiplier: 0, constant: w)
cell.lblTitle.addConstraint(constraints)
let kl = UIImage(imageLiteral: "bubble")
let imageView = UIImageView(frame:CGRectMake(0, 0, w, h))
imageView.image = kl
imageView.alpha = 0.4
cell.lblTitle.addSubview(imageView)
cell.lblTitle.sendSubviewToBack(imageView)
}
【问题讨论】:
标签: ios swift uitableview uiimageview autolayout