23

以编程方式更新表格时如何显示 UIRefreshControl?使用 [self.refreshControl beginRefreshing] 使微调器动画但不显示它。

4

4 回答 4

20

您必须自己手动更改contentOffsetUITableView一定要考虑到contentInset.top。它应该很简单:

CGPoint newOffset = CGPointMake(0, -[myTableView contentInset].top);
[myTableView setContentOffset:newOffset animated:YES];
于 2012-12-31T23:48:32.160 回答
18

这会成功的

- (void)beginRefreshingTableView {

    [self.refreshControl beginRefreshing];

    // check if contentOffset is zero
    if (fabsf(self.tableView.contentOffset.y) < FLT_EPSILON) {

        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^(void){

            self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);

        } completion:^(BOOL finished){

        }];

    }
}
于 2013-04-27T10:08:30.987 回答
9

对于 Swift 3,这是我根据 Peter Lapisu 的回答得出的:

override func viewDidLoad() {
    super.viewDidLoad()
    self.refreshControl?.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged)
    // ...
}

func refresh(sender:AnyObject) {
    self.refreshControl?.beginRefreshing()

    if let yOffsetTable = self.tableView?.contentOffset.y {
        if yOffsetTable < CGFloat(Float.ulpOfOne) {
            UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: {
                if let refreshControlHeight = self.refreshControl?.frame.height {
                    self.tableView?.contentOffset = CGPoint(x: 0, y: -refreshControlHeight)
                }
            }, completion: nil)
        }
    }
}
于 2016-02-15T00:52:39.833 回答
5

对于 Swift 5,这是我唯一的工作版本。

extension UIRefreshControl {

    func beginRefreshingManually() {
        if let scrollView = superview as? UIScrollView {
            scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.contentOffset.y - frame.height), animated: false)
        }
        beginRefreshing()
        sendActions(for: .valueChanged)
    }

}
于 2019-04-14T10:42:53.033 回答