大佬教程收集整理的这篇文章主要介绍了ios – TableView在滚动到另一个时折叠单元格:奇怪的行为,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果字符数超过250,则会缩短这些气泡
如果用户点击了气泡
>取消选择(缩短)先前的选择
>新选择扩展并显示整个内容
>新选择顶部约束更改(从0到4)
我想要实现什么?
没有这个滚动,contentOffset保持不变,看起来很糟糕.
(在视频中:右侧)
视频:
问题出现了:
在左边,你可以注意到它是毛病.
>随机幽灵细胞无缘无故地出现.
>有时甚至会弄乱一些气泡的高度(不在视频中)
为什么会这样?
码:
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项目
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,请注明来意。