最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]tableview中setContentOffset不工作

iOS crifan 6404浏览 0评论

折腾:

[基本解决]swift中在UITableView顶部显示正在加载的指示

期间,现在改成viewDidAppear中显示了,而去也去设置setContentOffset,并scrollRectToVisible:

  override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)

       

        updateIndicator()

    }

   

    func updateIndicator(){

        gLog.debug("self.stateInfo.curState=\(self.stateInfo.curState)")

        if self.stateInfo.curState == .WaitingMessage {

            //add loading indicator

           

            UIView.setAnimationsEnabled(false)

            var curTvContentOffset = self.messageTableView.contentOffset

            gLog.debug("curTvContentOffset=\(curTvContentOffset)")

            curTvContentOffset.y += LoadingIndicatorHeight

            gLog.debug("tableViewContentOffset=\(curTvContentOffset)")

            self.messageTableView.setContentOffset(curTvContentOffset, animated: false)

            loadingIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray

            loadingIndicatorView.hidesWhenStopped = true

            self.messageTableView.addSubview(loadingIndicatorView)

            constrain(loadingIndicatorView) {aiView in

                aiView.centerX == aiView.superview!.centerX

                aiView.top == aiView.superview!.top + LoadingIndicatorHeight / 2

                aiView.height == LoadingIndicatorHeight

            }

           

            //for debug

            loadingIndicatorView.backgroundColor = UIColor.greenColor()

            self.messageTableView.scrollRectToVisible(CGRectMake(0, 0, self.view.frame.width, LoadingIndicatorHeight), animated: false)

            UIView.setAnimationsEnabled(true)

           

            loadingIndicatorView.startAnimating()

结果还是不行:

tableview顶部还是没有多出来区域,然后indicator还是在当前页面,而不是上面显示的

搜:

tableview setContentOffset not work

UITableView contentOffset jumps after call to s… | Apple Developer Forums

说是:row为0时,没法滚动,虽然设置了setContentOffset

iphone – UITableView contentOffSet is not working properly – Stack Overflow

setcontentoffset tableview no cell

tableview setcontentoffset not working

此处,估计是content size还是旧的值,所以此处的contentOffset没起效果

去试试,设置contentSize

结果设置了contentSize还是不行:

    override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)

       

        updateIndicator()

    }

   

    func updateIndicator(){

        gLog.debug("self.stateInfo.curState=\(self.stateInfo.curState)")

        if self.stateInfo.curState == .WaitingMessage {

            //add loading indicator

           

            UIView.setAnimationsEnabled(false)

            var curTvContentOffset = self.messageTableView.contentOffset

            gLog.debug("curTvContentOffset=\(curTvContentOffset)") //(0.0, 0.0)

            curTvContentOffset.y += LoadingIndicatorHeight

            gLog.debug("tableViewContentOffset=\(curTvContentOffset)") //(0.0, 40.0)

           

            loadingIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray

            loadingIndicatorView.hidesWhenStopped = true

            self.messageTableView.addSubview(loadingIndicatorView)

            constrain(loadingIndicatorView) {aiView in

                aiView.centerX == aiView.superview!.centerX

                aiView.top == aiView.superview!.top + LoadingIndicatorHeight / 2

                aiView.height == LoadingIndicatorHeight

            }

           

            gLog.debug("self.messageTableView.contentSize=\(self.messageTableView.contentSize)") //(320.0, 62.0)

            self.messageTableView.contentSize.height += LoadingIndicatorHeight

            gLog.debug("self.messageTableView.contentSize=\(self.messageTableView.contentSize)") //(320.0, 102.0)

           

            self.messageTableView.setContentOffset(curTvContentOffset, animated: false)

            //for debug

            loadingIndicatorView.backgroundColor = UIColor.greenColor()

            self.messageTableView.scrollRectToVisible(CGRectMake(0, 0, self.view.frame.width, LoadingIndicatorHeight), animated: false)

            UIView.setAnimationsEnabled(true)

           

            gLog.debug("self.messageTableView.frame=\(self.messageTableView.frame)") //(0.0, 0.0, 320.0, 460.0)

            gLog.debug("self.messageTableView.contentSize=\(self.messageTableView.contentSize)") //(320.0, 102.0)

            gLog.debug("self.messageTableView.contentOffset=\(self.messageTableView.contentOffset)") //(0.0, 0.0)

            gLog.debug("self.messageTableView.contentInset=\(self.messageTableView.contentInset)") //UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

           

            loadingIndicatorView.startAnimating()

最终contentOffset还是0

tableview setContentOffset not work

UIscrollview setcontentoffset – 上善若水 – 博客频道 – CSDN.NET

-》后续如果实在不行,那就去借用:

tableview的header去用于指示器的显示

解决iOS开发中调用UIScrollView或UITableView的setContentOffset方法产生的抖动 – uniEagle

UIScrollView & setContentOffset random scrolling on iOS 4

UITableView – scroll to the topHowWayDo | HowWayDo

How to scroll to the bottom of a UITableView on the iPhone before the view appearsHowWayDo | HowWayDo

UITableView setContentOffset not working in viewDidLoad – iphone

tableview viewDidAppear setContentOffset not work

tableview viewWillAppear setContentOffset not work

ios – UIScrollView Setting contentoffset in viewwillappear not working – Stack Overflow

此处不想要去用:viewDidLayoutSubviews

-》因为别处添加删除cell等各种原因,都可能调用此方法

-》就可能重复加入对应的indicator了。。。

iphone – UITableView + Add content offset at top – Stack Overflow

tableview add top empty

ios – why extra space is at top of UITableView – simple – Stack Overflow

ios – Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7 – Stack Overflow

最后还是没解决,没法在:

viewDidAppear或viewWillAppear

中,去让tableview顶部通过setContentOffset额外添加一个显示区域

最后暂时去使用tableHeaderView达到类似的效果。

详见:

[基本解决]swift中在UITableView顶部显示正在加载的指示

转载请注明:在路上 » [已解决]tableview中setContentOffset不工作

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.181 seconds, using 22.11MB memory