TableView单元格再次显示

时间:2016-08-10 23:08:44

标签: ios swift uitableview

我有一个问题,如果我的表视图向下滚动足够,它将重复表视图上的第一个元素。老实说,我不知道为什么会这样做,我唯一的猜测是,我需要以某种方式读取该单元格以前存在但是在我已经看过的教程中,它们并非如此有那个问题。当实现cellForRowAtIndex路径方法时,它只是工作,不会重复单元格。

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "chatbubbleCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ChatBubbleCell

    /* Configure the cell */
    var yAxis: CGFloat = 5.0
    // Setup the chat bubble view
    if outgoingSender {
        let outgoingChatBubbleData = ChatBubbleData(text: messagesArray[indexPath.row], image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Mine)
        let outgoingChatBubble = ChatBubble(data: outgoingChatBubbleData, startY: yAxis)
        cell.contentView.addSubview(outgoingChatBubble)
    } else {
        let incomingChatBubbleData = ChatBubbleData(text: messagesArray[indexPath.row], image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Opponent)
        var incomingChatBubble = ChatBubble(data: incomingChatBubbleData, startY: yAxis)
        cell.contentView.addSubview(incomingChatBubble)
    }

    return cell
}

正如您所看到的,我正在检查传入的消息是传出还是传入,然后只是初始化包含标签的自定义视图,然后我只是将其添加到单元格的内容视图中。

任何帮助将不胜感激,谢谢。

- 保罗在下面告诉我,细胞正在被重复使用,这就是为什么它会出现同样的情况。所以我将我的单元逻辑放入我的原型单元类中,如图所示

   let outgoingChatBubbleData = ChatBubbleData(text: "", image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Mine)
    let incomingChatBubbleData = ChatBubbleData(text: "", image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Opponent)

然后在cellForRowAtIndexPath

中设置其属性
if outgoingSender {
    cell.outgoingChatBubbleData.text = messagesArray[indexPath.row]
    var incomingChatBubble = ChatBubble(data: cell.outgoingChatBubbleData, startY: yAxis)
    // Attach to bubble view?
} else {
    cell.incomingChatBubbleData.text = messagesArray[indexPath.row]
    let outgoingChatBubble = ChatBubble(data: cell.incomingChatBubbleData, startY: yAxis)
     // Attach to bubble view?
}

但现在我的问题是,我不确定如何附加" ChatBubble"对于单元格类型为UIView而不使用cellForRowAtIndexPath方法中的addSubview()

1 个答案:

答案 0 :(得分:0)

一种棘手的方法是在单元格初始化时添加子视图,并为isHidden中的子视图设置cellForRowAtIndexPath属性。你可能会得到你想要的东西。

相关问题