使用UIRefreshControl禁用UITableView中的底部反弹

时间:2015-10-27 13:24:23

标签: ios swift uitableview uirefreshcontrol

我想禁用UITableView底部反弹,我无法使用标准"反弹"故事板中的属性,bcs UIRefreshControl需要顶级弹跳才能工作。所以我尝试了this solution

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height - scrollView.frame.size.height)];
}

}

如果内容高度大于屏幕大小,它可以正常工作,但在其他情况下崩溃: enter image description here

正如你在上面看到的那样,UIRefreshControl没有被隐藏(如果UIRefreshControll被隐藏,那么uitableview中只有3个单元格,底部有空白区域)。我试图通过向我的方法添加UIRefreshControl高度来解决这个问题,但它没有帮助:

override func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height - self.refreshControl!.frame.size.height {
        scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentSize.height - scrollView.frame.size.height + self.refreshControl!.frame.size.height), animated: false)
    }
}

我在顶部有空格,RefreshControl停止工作:

enter image description here

我做错了什么? THX。

P.S。我的viewDidLoad方法:

override func viewDidLoad() {
    super.viewDidLoad()

    self.refreshControl = UIRefreshControl()
    self.refreshControl!.attributedTitle = NSAttributedString(string: "Обновление")
    self.refreshControl!.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
    self.edgesForExtendedLayout = UIRectEdge.None;
    tableView.allowsMultipleSelectionDuringEditing = false;
    tableView.tableFooterView = UIView()
    // menu button
    if self.revealViewController() != nil {
        menuButton.target = self.revealViewController()
        menuButton.action = "revealToggle:"
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
}

1 个答案:

答案 0 :(得分:4)

目前我找到了相当愚蠢的解决方案:

override func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentSize.height > self.view.frame.size.height && scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height {
        scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentSize.height - scrollView.frame.size.height), animated: false)
    }
}

我只在contentSize.height > self.view.frame.size.height时禁用弹跳,希望some1知道如何做得更好。