我正在为Apple-tv编写聊天应用程序,我在单元格聊天中显示自定义视图时遇到问题。
我有代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
UICollectionViewCell {
var myCell = self.chatCollectionView.dequeueReusableCellWithReuseIdentifier(chatCellIdentifier, forIndexPath: indexPath) as UICollectionViewCell
myCell.removeFromSuperview()
currentIndexPath = indexPath
if flag[indexPath.row]==0
{
myCell = sendBotMessage(historyString[indexPath.row], path: indexPath, collectionView: chatCollectionView)
}
else
{
myCell = sendUserMessage(historyString[indexPath.row], path: indexPath, collectionView: chatCollectionView)
}
return myCell
}
代码显示两个版本的消息,可以看作sendBotMessage
和sendUserMessage
。问题是下一个单元格的显示先前是错误的。首先使用1 sendBotMessage-view运行
并在添加第二项后
第二(右)消息按sendUserMessage
添加,另一个视图。
“对应”的整个历史存储在historyString
- 一个类型字符串数组中。正确组织存储,因为滚动显示错误消失。因此,可以得出关于渲染函数的正确性的结论
在调用historyString
和reloadData()
之后,所有新邮件都会附加在collectionView
中,并且必须重新绘制。
我可能犯错误的地方?我试图清除subview
collectionView
,禁用滚动动画,但没有帮助。
程序代码图纸视图:
func sendUserMessage(text : String, path : NSIndexPath, collectionView : UICollectionView) -> UICollectionViewCell {
let userMessage = text
let textSize = getTextFrameSize(userMessage)
let frameForMessage = getFrameforView(textSize)
let messageBuble = Chat_ViewInCell(frame: frameForMessage, textSize: textSize, text: userMessage)
messageBuble.backgroundColor = UIColor.clearColor()
let avatarImage = UIImage(named: "\(userAvatar+1).png")
let avatarView = UIImageView(image: avatarImage)
avatarView.contentMode = UIViewContentMode.ScaleAspectFit
let totalX = messageBuble.frame.width + 340
let constraint = 1750 - totalX
messageBuble.frame.origin = CGPointMake(constraint, 0)
avatarView.frame = CGRectMake(messageBuble.frame.maxX + 10, 0, 170, 170)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatCellIdentifier, forIndexPath: path) as! chatCollectionViewCell
//добавляем, счастье, радуемся
cell.addSubview(messageBuble)
cell.addSubview(avatarView)
return cell
}
func sendBotMessage(text : String, path : NSIndexPath, collectionView : UICollectionView) -> UICollectionViewCell {
let userMessage = text
let textSize = getTextFrameSize(userMessage)
let frameForMessage = getFrameforView(textSize)
let messageBuble = bot_ViewInCell(frame: frameForMessage, textSize: textSize, text: userMessage)
messageBuble.backgroundColor = UIColor.clearColor()
let avatarImage = UIImage(named: partner_image)
let avatarView = UIImageView(image: avatarImage)
avatarView.contentMode = UIViewContentMode.ScaleAspectFit
avatarView.frame = CGRectMake(0, 0, 170, 170)
let constraint = avatarView.frame.maxX + 10
messageBuble.frame.origin = CGPointMake(constraint, 0)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatCellIdentifier, forIndexPath: path) as! chatCollectionViewCell
//добавляем в возвращаемую ячейку
cell.addSubview(avatarView)
cell.addSubview(messageBuble)
return cell
}
使用自定义CollectionCellView推荐更改后,我有以下代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
UICollectionViewCell {
var myCell = chatCollectionView.dequeueReusableCellWithReuseIdentifier(chatCellIdentifier, forIndexPath: indexPath) as! chatCollectionViewCell
var customCell = myCell
if flag[indexPath.row]==0
{
customCell = sendBotMessage(historyString[indexPath.row], path: indexPath, collectionView: collectionView)
}
else
{
customCell = sendUserMessage(historyString[indexPath.row], path: indexPath, collectionView: collectionView)
}
myCell = customCell
currentIndexPath = indexPath
return myCell
}
绘图功能也有自定义单元格的更改:
func sendUserMessage(text : String, path : NSIndexPath, collectionView : UICollectionView) -> chatCollectionViewCell {
let userMessage = text
let textSize = getTextFrameSize(userMessage) //высчитали необходимые размеры лейбла
let frameForMessage = getFrameforView(textSize) //высчиитали необходимые размеры всей вьюшки
let messageBuble = Chat_ViewInCell(frame: frameForMessage, textSize: textSize, text: userMessage)
messageBuble.backgroundColor = UIColor.clearColor()
//создаем аватар
let avatarImage = UIImage(named: "\(userAvatar+1).png")
let avatarView = UIImageView(image: avatarImage)
avatarView.contentMode = UIViewContentMode.ScaleAspectFit
//считаем отступ до правого края, позицию аватара
let totalX = messageBuble.frame.width + 340
let constraint = 1750 - totalX
messageBuble.frame.origin = CGPointMake(constraint, 0)
avatarView.frame = CGRectMake(messageBuble.frame.maxX + 10, 0, 170, 170)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatCellIdentifier, forIndexPath: path) as! chatCollectionViewCell
//добавляем, счастье, радуемся
cell.addSubview(messageBuble)
cell.addSubview(avatarView)
return cell
}
func sendBotMessage(text : String, path : NSIndexPath, collectionView : UICollectionView) -> chatCollectionViewCell {
let userMessage = text
let textSize = getTextFrameSize(userMessage) //высчитали необходимые размеры лейбла
let frameForMessage = getFrameforView(textSize) //высчиитали необходимые размеры всей вьюшки
//создали всю вьюшку
let messageBuble = bot_ViewInCell(frame: frameForMessage, textSize: textSize, text: userMessage)
messageBuble.backgroundColor = UIColor.clearColor()
//создаем аватар и считаем его позицию
let avatarImage = UIImage(named: partner_image)
let avatarView = UIImageView(image: avatarImage)
avatarView.contentMode = UIViewContentMode.ScaleAspectFit
avatarView.frame = CGRectMake(0, 0, 170, 170)
let constraint = avatarView.frame.maxX + 10
messageBuble.frame.origin = CGPointMake(constraint, 0)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatCellIdentifier, forIndexPath: path) as! chatCollectionViewCell
//добавляем в возвращаемую ячейку
cell.addSubview(avatarView)
cell.addSubview(messageBuble)
return cell
}
但问题没有修复......我自定义CollectionViewCell出了什么问题?我必须做另一个吗?
答案 0 :(得分:0)
问题是解决的!我不了解prepareForReuse()方法。如此简单,在自定义单元类defeniton中我必须覆盖它的方法,并调用子视图clearsContextBeforeDrawing和removeFromSuperview。代码在这里:
class chatCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var view: Chat_ViewInCell!
override func prepareForReuse() {
super.prepareForReuse()
for currView in self.subviews
{
currView.clearsContextBeforeDrawing = true
currView.removeFromSuperview()
}
}
一切正常,没有错误的重绘。