Swift 为不同视图创建布局时出现问题
我正在构建一个tableview,它在不同的tableview单元格中具有不同的视图,这取决于它从模型接收到的输入,即如果文本出来,则只有一个文本字段,或者如果图像出来,则只有一个图像。我以编程方式编程视图,并设置了约束条件。当我运行程序时,我看到了我所期望的,但当我上下滚动时,一切都变得一团糟。当我滚动更多的时候,一切都变得一样,不管它应该包含什么。我知道我做错了什么,我可能不得不使用prepareforeuse,但我不知道如何解决这个问题。非常感谢您的任何意见。非常感谢你。这是我的代码片段 var otherUserProfileImageLeftConstraint:NSLayoutConstraintSwift 为不同视图创建布局时出现问题,swift,uitableview,Swift,Uitableview,我正在构建一个tableview,它在不同的tableview单元格中具有不同的视图,这取决于它从模型接收到的输入,即如果文本出来,则只有一个文本字段,或者如果图像出来,则只有一个图像。我以编程方式编程视图,并设置了约束条件。当我运行程序时,我看到了我所期望的,但当我上下滚动时,一切都变得一团糟。当我滚动更多的时候,一切都变得一样,不管它应该包含什么。我知道我做错了什么,我可能不得不使用prepareforeuse,但我不知道如何解决这个问题。非常感谢您的任何意见。非常感谢你。这是我的代码片段
var otherUserProfileImageBottomConstraint : NSLayoutConstraint!
var otherUserProfileImageHeightConstraint : NSLayoutConstraint!
var otherUserProfileImageWidthConstraint : NSLayoutConstraint!
// rightViewforholiding
var rightViewLeftConstraintN : NSLayoutConstraint!
var rightViewBottomConstraintN : NSLayoutConstraint!
var rightViewHeightConstraintN : NSLayoutConstraint!
var rightViewWidthConstraintN : NSLayoutConstraint!
//imageInside the rightView
var messageImageImageWidthConstraint : NSLayoutConstraint!
var messageImageImageHeightConstraint : NSLayoutConstraint!
// playButton
var playButRightConstraint : NSLayoutConstraint!
var playButTopConstraint : NSLayoutConstraint!
var playButWidthtConstraint : NSLayoutConstraint!
var playButHeightConstraint : NSLayoutConstraint!
// activity indicator
var activityXAnchorConstraint : NSLayoutConstraint!
var activityYAnchorConstraint : NSLayoutConstraint!
var activityHeightAnchorConstraint : NSLayoutConstraint!
var activityWidthAnchorConstraint : NSLayoutConstraint!
// time stamp
var timeStampTopConstraint : NSLayoutConstraint!
var timeStampBottomConstraint : NSLayoutConstraint!
var timeStampLeftConstraint : NSLayoutConstraint!
var timeStampRightConstraint : NSLayoutConstraint!
// text label
var textLabelWidthConstraint : NSLayoutConstraint!
var textLabelHeightConstraintN : NSLayoutConstraint!
var Chatchellin:messageIn{
didSet {
let textToShow = chatCellIn?.text
if chatCellIn?.from != Auth.auth().currentUser?.uid {
// MARK: New implementation start
// THIS IS THE OTHER USER
if textToShow == "" {
// we have either the video or the image coming in
let videoURL = chatCellIn?.videoURL
let pictureURL = chatCellIn?.imageURL
if videoURL != nil && pictureURL != nil {
// this is where we have the video
// we need image on the rightview - that contains just the image and the time label + userLeftImage
otherUserProfileImageLeftConstraint.isActive = true
otherUserProfileImageWidthConstraint.isActive = true
otherUserProfileImageHeightConstraint.isActive = true
otherUserProfileImageBottomConstraint.isActive = true
rightViewLeftConstraintN.isActive = true
rightViewBottomConstraintN.isActive = true
rightViewHeightConstraintN.isActive = true
rightViewWidthConstraintN.isActive = true
messageImageImageHeightConstraint.isActive = true
messageImageImageWidthConstraint.isActive = true
timeStampRightConstraint.isActive = true
timeStampLeftConstraint.isActive = true
timeStampTopConstraint.isActive = true
timeStampBottomConstraint.isActive = true
} else if videoURL == nil && pictureURL != nil {
// this is where we have the picture
// we need just the image and the time label and the activity indictor and the play button + userLeftImage
otherUserProfileImageLeftConstraint.isActive = true
otherUserProfileImageWidthConstraint.isActive = true
otherUserProfileImageHeightConstraint.isActive = true
otherUserProfileImageBottomConstraint.isActive = true
rightViewLeftConstraintN.isActive = true
rightViewBottomConstraintN.isActive = true
rightViewHeightConstraintN.isActive = true
rightViewWidthConstraintN.isActive = true
messageImageImageHeightConstraint.isActive = true
messageImageImageWidthConstraint.isActive = true
timeStampRightConstraint.isActive = true
timeStampLeftConstraint.isActive = true
timeStampTopConstraint.isActive = true
timeStampBottomConstraint.isActive = true
}
} else {
// we have the text here
// we need just the text label and the time label + userLeftImage
otherUserProfileImageLeftConstraint.isActive = true
otherUserProfileImageWidthConstraint.isActive = true
otherUserProfileImageHeightConstraint.isActive = true
otherUserProfileImageBottomConstraint.isActive = true
rightViewLeftConstraintN.isActive = true
rightViewBottomConstraintN.isActive = true
rightViewHeightConstraintN.isActive = true
rightViewWidthConstraintN.isActive = true
messageImageImageHeightConstraint.isActive = false
messageImageImageWidthConstraint.isActive = false
timeStampRightConstraint.isActive = false
timeStampLeftConstraint.isActive = false
timeStampTopConstraint.isActive = false
timeStampBottomConstraint.isActive = false
}
} else {
//
////标记:此消息来自当前用户
// MARK: NEW IMPLEMENTATION START
if textToShow == "" {
// we have either the video or the image coming in
let videoURL = chatCellIn?.videoURL
let pictureURL = chatCellIn?.imageURL
if videoURL != nil && pictureURL != nil {
// this is where we have the video
// we need image on the rightview - that contains just the image and the time label + userLeftImage
otherUserProfileImageLeftConstraint.isActive = true
otherUserProfileImageWidthConstraint.isActive = true
otherUserProfileImageHeightConstraint.isActive = true
otherUserProfileImageBottomConstraint.isActive = true
rightViewLeftConstraintN.isActive = false
rightViewBottomConstraintN.isActive = false
rightViewHeightConstraintN.isActive = false
rightViewWidthConstraintN.isActive = false
messageImageImageHeightConstraint.isActive = false
messageImageImageWidthConstraint.isActive = false
timeStampRightConstraint.isActive = false
timeStampLeftConstraint.isActive = false
timeStampTopConstraint.isActive = false
timeStampBottomConstraint.isActive = false
} else if videoURL == nil && pictureURL != nil {
// this is where we have the picture
// we need just the image and the time label and the activity indictor and the play button + userLeftImage
otherUserProfileImageLeftConstraint.isActive = true
otherUserProfileImageWidthConstraint.isActive = true
otherUserProfileImageHeightConstraint.isActive = true
otherUserProfileImageBottomConstraint.isActive = true
rightViewLeftConstraintN.isActive = false
rightViewBottomConstraintN.isActive = false
rightViewHeightConstraintN.isActive = false
rightViewWidthConstraintN.isActive = false
messageImageImageHeightConstraint.isActive = false
messageImageImageWidthConstraint.isActive = false
timeStampRightConstraint.isActive = false
timeStampLeftConstraint.isActive = false
timeStampTopConstraint.isActive = false
timeStampBottomConstraint.isActive = false
}
} else {
// we have the text here
// we need just the text label and the time label + userLeftImage
otherUserProfileImageLeftConstraint.isActive = true
otherUserProfileImageWidthConstraint.isActive = true
otherUserProfileImageHeightConstraint.isActive = true
otherUserProfileImageBottomConstraint.isActive = true
rightViewLeftConstraintN.isActive = false
rightViewBottomConstraintN.isActive = false
rightViewHeightConstraintN.isActive = false
rightViewWidthConstraintN.isActive = false
messageImageImageHeightConstraint.isActive = false
messageImageImageWidthConstraint.isActive = false
timeStampRightConstraint.isActive = false
timeStampLeftConstraint.isActive = false
timeStampTopConstraint.isActive = false
timeStampBottomConstraint.isActive = false
}
}
}
}
func newAddview(){
重写init(样式:UITableViewCell.CellStyle,reuseIdentifier:String?){
init(样式:style,reuseIdentifier:reuseIdentifier)
//对于tableview实现
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell = chatTableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! ChatSceneTableViewCell
cell.chatCellIn = messageArrayToDisplay[indexPath.row]
return cell
}
newAddview()
}
let cell = chatTableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! ChatSceneTableViewCell
cell.chatCellIn = messageArrayToDisplay[indexPath.row]
return cell
}