HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – TableView在滚动到另一个时折叠单元格:奇怪的行为大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带聊天气泡的tableView.

如果字符数超过250,则会缩短这些气泡

如果用户点击了气泡

>取消选择(缩短)先前的选择
>新选择扩展并显示整个内容
>新选择顶部约束更改(从0到4)

我想要实现什么?

我会分享一个关于它的视频

没有这个滚动,contentOffset保持不变,看起来很糟糕.

(在视频中:右侧)

视频:

https://youtu.be/_-peZycZEAE

问题出现了:

在左边,你可以注意到它是毛病.

>随机幽灵细胞无缘无故地出现.
>有时甚至会弄乱一些气泡的高度(不在视频中)

为什么会这样?

码:

func bubbleTappedHandler(sender: UITapGestureRecognizer) {

        let touchPoint = sender.LOCATIOn(in: self.tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {

            if indexPath == currentSELEctedIndexPath {

                // SELEcted bubble is tapped,deSELEct it
                self.SELEctDeSELEctBubbles(deSELEct: indexPath)

            } else {
                if (currentSELEctedIndexPath != nil){

                    // DeSELEct old bubble,SELEct new one
                    self.SELEctDeSELEctBubbles(SELEct: indexPath,deSELEct: currentSELEctedIndexPath)

                } else {

                    // SELEct bubble
                    self.SELEctDeSELEctBubbles(SELEct: indexPath)

                }
            }

        }
    }



    func SELEctDeSELEctBubbles(SELEct: IndexPath? = nil,deSELEct: IndexPath? = nil){


        var deSELEctCell : WorldmessageCell?
        var SELEctCell : WorldmessageCell?

        if let deSELEct = deSELEct {
            deSELEctCell = tableView.cellForRow(at: deSELEct) as? WorldmessageCell
        }
        if let SELEct = SELEct {
            SELEctCell = tableView.cellForRow(at: SELEct) as? WorldmessageCell
        }


        // DeSELEct Main
        if let deSELEct = deSELEct,let deSELEctCell = deSELEctCell {

            tableView.deSELEctRow(at: deSELEct,animated: falsE)
            currentSELEctedIndexPath = nil
            // Update text
            deSELEctCell.messageLabel.text = self.datasource[deSELEct.row].message.shortened()


        }
        // SELEct Main
        if let SELEct = SELEct,let SELEctCell = SELEctCell {

            tableView.SELEctRow(at: SELEct,animated: true,scrollPosition: .nonE)
            currentSELEctedIndexPath = SELEct
            // Update text
            deSELEctCell.messageLabel.text = self.datasource[SELEct.row].message.full()
        }


        UIView.animate(withDuration: appSetTings.defaultAnimationSpeed) {

            // DeSELEct Cons@R_696_9829@nt changes

            if let deSELEct = deSELEct,let deSELEctCell = deSELEctCell {
                // Constarint change
                deSELEctCell.nickNameButtonTopCons@R_696_9829@nt.constant = 0
                deSELEctCell.timeLabel.alpha = 0.0
                deSELEctCell.layoutIfNeeded()

            }

            // SELEct Cons@R_696_9829@nt changes
            if let SELEct = SELEct,let SELEctCell = SELEctCell {

                // Constarint change
                SELEctCell.nickNameButtonTopCons@R_696_9829@nt.constant = 4
                SELEctCell.timeLabel.alpha = 1.0
                SELEctCell.layoutIfNeeded()


            }


        }

        self.tableView.beginupdates()
        self.tableView.endupdates()



        UIView.animate(withDuration: appSetTings.defaultAnimationSpeed) {
            if let SELEct = SELEct,deSELEct != nil,self.tableView.cellForRow(at: deSELEct!) == nil && deSELEctCell != nil {

                // if DeSELEcted row is not anymore on screen
                // but was before the collapsing,// then scroll to new SELEcted row  

                self.tableView.scrollToRow(at: SELEct,at: .top,animated: falsE)
            }
        }

    }

更新1:添加了Github项目

链接https://github.com/krptia/test2

我制作了一个小版本的应用程序,以便您可以查看并测试我的问题所在.如果有人能帮忙解决这个问题,我会非常感激! :C

解决方法

首先让我们通过“不滚动”来定义我们的意思 – 我们的意思是更少的细胞保持不变.所以我们想找到一个我们想成为锚细胞的细胞.从更改之前到更改之后,从单元格的顶部到屏幕顶部的距离是相同的.

var indexPathAnchorPoint:IndexPath?
var offsetAnchorPoint:CGFloat?

func findHighestCellThatStartsInFrame() -> UITableViewCell? {
  var anchorCell:UITableViewCell?
  for cell in self.tableView.visibleCells {
    let topIsInFrame = cell.frame.origin.y >= self.tableView.contentOffset.y
    if topIsInFrame {

      if let currentlySELEcted = anchorCell{
        let isHigerUpInView = cell.frame.origin.y < currentlySELEcted.frame.origin.y
        if  isHigerUpInView {
          anchorCell = cell
        }
      }else{
        anchorCell = cell    
      }
    }
  }
  return anchorCell
}

func setAnchorPoint() {
  self.indexPathAnchorPoint = nil;
  self.offsetAnchorPoint = nil;

  if let cell = self.findHighestCellThatStartsInFrame() {
    self.offsetAnchorPoint = cell.frame.origin.y - self.tableView.contentOffset.y
    self.indexPathAnchorPoint = self.tableView.indexPath(for: cell)
  }
}

我们在开始做之前先调用它.

func bubbleTappedHandler(sender: UITapGestureRecognizer) {
    self.setAnchorPoint()
     ....

接下来,我们需要在进行更改后设置内容偏移量,以便单元格移回到它所设想的位置.

func scrollToAnchorPoint() {
  if let indexPath = indexPathAnchorPoint,let offset = offsetAnchorPoint {
    let rect = self.tableView.rectForRow(at: indexPath)
    let contentOffset = rect.origin.y - offset
    self.tableView.setContentOffset(CGPoint.init(x: 0,y: contentOffset),animated: falsE)
  }
}

接下来我们在完成更改后调用它.

self.tableView.beginupdates()
  self.tableView.endupdates()
  self.tableView.layoutSubviews()
  self.scrollToAnchorPoint()

动画可能有点奇怪,因为有很多东西在同一时间发生.我们正在同时更改内容偏移量和单元格的大小,但如果您将手指放在第一个单元格旁边,顶部可见,您将看到它最终位于正确的位置.

大佬总结

以上是大佬教程为你收集整理的ios – TableView在滚动到另一个时折叠单元格:奇怪的行为全部内容,希望文章能够帮你解决ios – TableView在滚动到另一个时折叠单元格:奇怪的行为所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。