ios - 如何在重新加载单元格后保留单元格中的 ScrollView 内容偏移量

标签 ios swift

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let post = posts[indexPath.item]
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CellClass
    cell.button.tag = indexPath.item
    cell.button.addTarget(self, action: #selector(didLike(sender:)), for: .touchUpInside) 
}



@objc func didLike(sender: UIButton) {
    let post = self.posts[sender.tag]
    let indexPath = IndexPath(item: sender.tag, section: 0)
    self.posts[sender.tag] = self.selectLikeButton(post: post)

    self.collectionView?.reloadItems(at: [indexPath])

}

细胞类

class NoticeLetterViewCell: UICollectionViewCell {

@IBOutlet weak var profileImage: CustomImageView!
@IBOutlet weak var userNameButton: UIButton!
@IBOutlet weak var optionButton: UIButton!
@IBOutlet weak var letterTextView: MyLetterStyleTextView!
@IBOutlet weak var itemButtons: UIStackView!
@IBOutlet weak var likeButton: UIButton!
@IBOutlet weak var commentButton: UIButton!
@IBOutlet weak var sendMessage: UIButton!
@IBOutlet weak var bookmark: UIButton!
@IBOutlet weak var likeCounter: UILabel!
@IBOutlet weak var captionLabel: UILabel!
@IBOutlet weak var timeAgo: UILabel!
@IBOutlet weak var startVoiceButton: UIButton!
@IBOutlet weak var commentViewButton: UIButton!
@IBOutlet weak var commentCountLabel: UILabel!
@IBOutlet weak var lastComment: UILabel!
@IBOutlet weak var goCommentView: UIButton!

@IBOutlet weak var pageStack: UIStackView!

@IBOutlet weak var scrollImageView: UIScrollView! {
    didSet {
        self.scrollImageView.delegate = self

    }
}

@IBOutlet weak var pageLeftView: UIView!
@IBOutlet weak var pageControl: FlexiblePageControl!
@IBOutlet weak var pageRightView: UIView!

 }


extension NoticeLetterViewCell: UIScrollViewDelegate {

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let page = Int(floor(scrollView.contentOffset.x / UIScreen.main.bounds.width))
    self.pageControl.setCurrentPage(at: page)

}

在 Collection View Controller 中的单元格中 使用 self.collectionView?.reloadItems(at: [indexPath]) 后,单元格中 ScrollView 的位置发生变化。我想保存原来的位置并使用它,但这太困难了。 TT

最佳答案

我真的不知道这是否可行,因为它很长且过于复杂,但我想不出其他任何东西。

View Controller

 var scrollArray = [Int]()
 var runbefore = false

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let post = posts[indexPath.item]
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CellClass
    cell.button.tag = indexPath.item
    cell.button.addTarget(self, action: #selector(didLike(sender:)), for: .touchUpInside) 

     cell.viewpassed = self
     cell.indexofCell = indexPath.row

     if scrollArray[indexPath.row] != nil {
         cell.movetoNumber = scrollArray[indexPath.row]
     }

     if runbefore == false {
         scrollArray.append(0)
         if indexPath.row = posts.count - 1 {
            runbefore = true
         }
     }
}

细胞类别

    extension NoticeLetterViewCell: UIScrollViewDelegate {

       var movetoNumber = 0
       var indexofCell = 0
       public weak var viewpassed : ViewController? //This is what class you have the cell for item at function

       override func awakeFromNib() {
            if movetoNumber != 0 {
             //Move the scrollView to movetoNumberPoint Run whatever action your Want HERE
            }
       }

       func scrollViewDidScroll(_ scrollView: UIScrollView) {
           let page = Int(floor(scrollView.contentOffset.x / UIScreen.main.bounds.width))
           if page != 0 {
                viewpassed.scrollArray.remove(at: indexofCell)
                viewpassed.scrollArray.insert(page, at: indexofCell)
           }
        }

    }

关于ios - 如何在重新加载单元格后保留单元格中的 ScrollView 内容偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54218752/

相关文章:

ios - tableView 数据源在 viewDidLoad() 中为空 - Swift

swift - Google 登录委托(delegate)没有被调用

ios - 设置 View Controller 不解雇

iOS - 从另一个类获取 NSMutableArray

ios - 如何决定在 Interface Builder 中哪个 View 应该位于顶部

ios - 附加 View Controller 中的导航栏

objective-c - 创建一个带有核心情节的发行版本?

ios - Pulltorefresh 只工作一次而不是多次

ios - 取消url请求以避免多次api调用

ios - ScrollView 中的按钮被禁用 - iOS Swift