0
我在故事板上创建了一个原型UITableViewCell
。它包含1个ImageView,3个用于显示内容的标签。所有的控件在UITableViewCell
的相应子类中都有网点。我显示可以是多行的消息。所以我不得不添加下面的方法文本在iOS中的uitableview单元格中重叠
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
标签的文本是越来越重叠,它会发生每当我滚动tableview。它只发生在前两行或最后一行。但是当我在谷歌上搜索时,我发现它可以随时发生在任何行上。这是我常用的扩展代码。
extension ConversationViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let count = selectedConversation?.msgArrayWithBodyText.count {
return count
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(conversationMessageIdentifier, forIndexPath: indexPath) as! ConversationMessageTableViewCell
let currMsg = selectedConversation.msgArrayWithBodyText[indexPath.row]
cell.userFullNameLabel.text = currMsg.senderName
cell.dateLabel.text = selectedConversation.getShortDateForMessage(currMsg.timeSent)
cell.messageBodyLabel.text = currMsg.bodyText //Constants.testString
let imageUrl = "\(Constants.serverUrl)/api/users/\(currMsg.senderId)/profilePicture"
if let item = ImageCache.sharedInstance.objectForKey(imageUrl) as? CacheableItem {
print("\(imageUrl) found in cache, expiresAt: \(item.expiresAt)")
cell.userImageView.image = UIImage(data: item)
} else {
cell.userImageView.image = UIImage(named: "Profile")
self.imageDownLoadingQueue.addOperationWithBlock({
if let url = NSURL(string: imageUrl) {
if let purgableData = NSPurgeableData(contentsOfURL: url) {
if let downloadedImage = UIImage(data: purgableData) {
print("cache: \(imageUrl)")
let item = CacheableItem()
item.setData(purgableData)
ImageCache.sharedInstance.setObject(item, forKey: imageUrl)
NSOperationQueue.mainQueue().addOperationWithBlock({
if let updateCell = tableView.cellForRowAtIndexPath(indexPath) as? ConversationMessageTableViewCell {
updateCell.userImageView.image = downloadedImage
}
})
}
}
}
})
}
//cell.clipsToBounds = true
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")) {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setLayoutMargins:")) {
cell.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
cell.preservesSuperviewLayoutMargins = false
}
cell.backgroundColor = UIColor.clearColor()
cell.backgroundView = UIView(frame:cell.frame)
if(cell.backgroundView?.layer.sublayers?.count > 1){
cell.backgroundView?.layer.sublayers?.removeAtIndex(0)
}
let layer = self.getGradientLayer(indexPath.row)
cell.backgroundView?.layer.insertSublayer(layer, atIndex: 0)
}
}
这里是我写的原型单元格的UITableViewCell
的子类。
class ConversationMessageTableViewCell: UITableViewCell {
@IBOutlet weak var userImageView: UIImageView!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var userFullNameLabel: UILabel!
@IBOutlet weak var messageBodyLabel: UILabel!
@IBOutlet weak var messageBodyLabelHeightConstraint: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
userImageView.layer.borderColor = UIColor.clearColor().CGColor
userImageView.layer.borderWidth = 1.0
userImageView.layer.cornerRadius = (userImageView.frame.height/2)
userImageView.clipsToBounds = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
下面是编辑的屏幕截图(我已经熏黑了的名字和面孔)
需要帮助
!我可以看到长文本没有被搞乱,但它只发生在小文本中。
基本上,这只发生在我上下滚动tableview几次。当tableview加载时,所有的文本都会正确显示,但随后出现混乱,然后清除一段时间,然后再混乱起来。
你是否向水平间距等标签添加了约束? –
是的,它增加了一切。限制和一切都很好。它只是重叠。 –
约束必须存在一些问题。你预期的产出是多少? –