重新加载单元格后如何在单元格中保持scrollview contentoffset

时间:2019-01-16 14:01:48

标签: 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])

}

CellClass

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)

}

在集合视图控制器中的单元格中 使用self.collectionView?.reloadItems(at:[indexPath])之后,滚动视图在单元格中的位置将发生变化。我想保存原始位置并使用它,但这太困难了。 T.T

1 个答案:

答案 0 :(得分:0)

我真的不知道这是否会因为它的时间太长和过于复杂而起作用,但我想不出其他任何事情。

ViewController

 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)
           }
        }

    }