Ios Uitable查看单元格复制问题swift

Ios Uitable查看单元格复制问题swift,ios,swift,uitableview,Ios,Swift,Uitableview,我正在创建一个聊天应用程序,其中我正在创建语音气泡。该问题发生在单元格上,因为它在滚动UITableView时重叠。我尝试过很多解决方案,但都不奏效 我的代码如下: var cell:chatBubble? = tableView.dequeueReusableCell(withIdentifier:"cell1") as? chatBubble if cell == nil {

我正在创建一个聊天应用程序,其中我正在创建语音气泡。该问题发生在单元格上,因为它在滚动UITableView时重叠。我尝试过很多解决方案,但都不奏效

我的代码如下:

var cell:chatBubble? = tableView.dequeueReusableCell(withIdentifier:"cell1") as? chatBubble

if cell == nil {
                                                   
    tableView.register(UINib.init(nibName: "chatBubble", bundle: nil), forCellReuseIdentifier: "cell1")

    let arrNib:Array = Bundle.main.loadNibNamed("chatBubble",owner: self, options: nil)!
    cell = arrNib.first as? chatBubble
}
let green = UIColor(red: 179/255, green: 52/255, blue: 199/255, alpha: 1)

if (arrCell.contains(indexPath.row)) {
    print("Exist")
} else {
    arrCell.append(indexPath.row)
    print("Not exist")
                                               
    cell!.showOutgoingMessage(color: green, text: self.animals[indexPath.row], tag: indexPath.row)
}
UITableviewCell


func showOutgoingMessage(color: UIColor, text: String, tag: Int) {
            
    lblmsg.numberOfLines = 0
    lblmsg.font = UIFont.systemFont(ofSize: 18)
    lblmsg.textColor = .white
    lblmsg.text = text
    lblmsg.tag = tag
                    
    let constraintRect = CGSize(width: 0.66 * self.frame.width,
                                                height: .greatestFiniteMagnitude)
    let boundingBox = text.boundingRect(with: constraintRect,
                      options: .usesLineFragmentOrigin,
                      attributes: [.font: lblmsg.font],
                      context: nil)
    lblmsg.frame.size = CGSize(width: ceil(boundingBox.width),
                               height: ceil(boundingBox.height))
                    
    let bubbleImageSize = CGSize(width: lblmsg.frame.width + 20,
                                 height: lblmsg.frame.height + 20)
                
    outgoingMessageView = UIImageView(frame:
                         CGRect(x: self.lblmsg.frame.origin.x,
                                y: self.lblmsg.frame.origin.y,
                                         width: bubbleImageSize.width,
                                         height: bubbleImageSize.height))
    outgoingMessageView.tag = tag
            
    let bubbleImage = UIImage(named: "incoming-message-bubble")?
                       
                        .resizableImage(withCapInsets: UIEdgeInsets(top: 17, left: 21, bottom: 17, right: 21),
                                        resizingMode: .stretch)
                        .withRenderingMode(UIImageRenderingMode.alwaysTemplate)
                    
                    outgoingMessageView.image = bubbleImage
                    outgoingMessageView.tintColor = color
            self.addSubview(outgoingMessageView)
    }
我使用了下面的函数来求解重复单元格,但它清除了 第1个和第8个电池(重复使用的电池)。我只是想把牢房里的事情弄清楚

override func prepareForReuse() {
    super.prepareForReuse()
    self.lblmsg.text = nil
    self.outgoingMessageView.image = nil    
} 
                        
[![cell repeat][1]][1]





  [1]: https://i.stack.imgur.com/FRAi4.png
 

不确定,这完全解决了您的问题,但在查看您的代码后,我有一些建议

  • 将nib注册移到cellForRow函数之外,例如查看控制器的加载:
  • override func viewDidLoad(){
    super.viewDidLoad()
    tableView.register(UINib.init(nibName:“chatbuble”,bundle:nil),forCellReuseIdentifier:“cell1”)
    }
    
  • 从更新dequeueReusableCell代码
  • var单元格:chatbuble?=tableView.dequeueReusableCell(标识符为“cell1”)作为?聊天泡泡
    如果单元格==nil{
    tableView.register(UINib.init(nibName:“chatbuble”,bundle:nil),forCellReuseIdentifier:“cell1”)
    让arrNib:Array=Bundle.main.loadNibNamed(“chatbuble”,所有者:self,选项:nil)!
    单元格=arrNib.first as?chatbuble
    }
    

    let cell=tableView.dequeueReusableCell(标识符为:“cell1”,for:indexath)as!聊天泡泡
    
  • 在出列周期中不执行reinit outgoingMessageView
  • outgoingMessageView=UIImageView(帧:
    CGRect(x:self.lblmsg.frame.origin.x,
    y:self.lblmsg.frame.origin.y,
    宽度:bubbleImageSize.width,
    高度:气泡图像大小。高度)
    
    改为更新帧

    outgoingMessageView.frame=CGRect(x:self.lblmsg.frame.origin.x,
    y:self.lblmsg.frame.origin.y,
    宽度:bubbleImageSize.width,
    高度:气泡图像大小。高度)
    
  • 不要每次都添加到视图层次结构中,因此请删除该行
  • self.addSubview(outgoingMessageView)

    或者在
    override func awakeFromNib()
    override func prepareforeuse()
    等中仅添加一次此outgoingMessageView

  • 确保不要将自动布局和基于帧的方法混合使用。只选择其中一个

  • 实际上,跟随一段代码会导致细胞复制

    if (arrCell.contains(indexPath.row)) {
        print("Exist")
    } else {
        arrCell.append(indexPath.row)
        print("Not exist")
                                                   
        cell!.showOutgoingMessage(color: green, text: self.animals[indexPath.row], tag: indexPath.row)
    }
    

    我删除了这段代码,显示了单元格重复

    好的,让我试试。谢谢,我已经将outgoing imageview添加为iboutlet,更改了一段代码,使单元格出现重复问题。我也应用了你的建议。谢谢Andrewy你没有提供足够的信息让人们能够帮助解决你所描述的问题。在这些额外的信息变得有用之前,这里还需要解决至少5个问题(在下面的答案中有详细说明),即使这样,可能还有5-10个问题。