Header Displaced in TableView with UIRefreshControl

前端 未结 5 1063
暗喜
暗喜 2020-12-09 08:33

My UIRefreshController is doing something odd. When I pull-down refresh, the tableView headers are displaced.

If I pull-down it looks fine, but if I scroll down the

相关标签:
5条回答
  • 2020-12-09 08:46

    a quick fix to this is to go like this

    Objective-C

    //header
    @property UITableViewController *tableController;
    
    //.m (right at the beginning of viewDidLoad for example)
    self.tableController = [[UITableViewController alloc] init];
    [self addChildViewController:self.tableController];
    self.tableController.tableView = self.tableView;
    
    ...
    
    //then create the refresh control and assign it to the UITableViewController
    self.tableController.refreshControl = refreshControl;
    

    Swift 2.1

    //Create an instance of a UITableViewController. This will host your UITableView.
    private let tableController = UITableViewController()
    
    //Add tableController as a childViewController and set its tableView property to your UITableView.
    self.addChildViewController(self.tableController)
    self.tableController.tableView = self.tableView
    self.refreshControl.addTarget(self, action: "refreshData:", forControlEvents: .ValueChanged)
    self.tableController.refreshControl = self.refreshControl
    

    this helps if you have your table hooked up to an IBOutlet and have other things linked into the storyboard you dont feel like messing with.

    0 讨论(0)
  • 2020-12-09 08:50

    @available(iOS 10.0, *)

    tableView.refreshControl = refreshControl

    0 讨论(0)
  • 2020-12-09 08:52

    UIRefreshControl's aren't meant to be subviews, they're meant to (literally) be the table's refresh control. UITableViewController has an outlet specifically for them (again, literally called refreshControl) that you should be using. As a subview of the table, you may be causing the table to assume it's a cell, rather than just a subview, which forces a recalculation around it. There will be cases where you do get lucky and the control may set itself in the right place, but this is, again, the result of undefined behavior.

    UITableViewController is not meant to be a limiting class, and it certainly should not keep you from implementing "multiple table views" (which sound context-specific enough that they'd warrant a new view controller presented anyhow). If you are worried about having to write boilerplate for each class, write an abstract superclass controller for every table view you want to implement, and subclass it as necessary.

    0 讨论(0)
  • 2020-12-09 08:54

    This could be an issue due to the fact that you are adding _refreshControl as a subview which is not supposed to be done. However you can create a UITableViewController object add it as the child view controller of your current viewcontroller class.

    For eg:-

    UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    [self addChildViewController:tableViewController];
    
    tableViewController.refreshControl = [[UIRefreshControl alloc] init];
    [tableViewController.refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    tableViewController.tableView.frame = CGRectMake(...);//set the frame here
    [self.view addSubview:tableViewController.tableView];
    
    0 讨论(0)
  • 2020-12-09 09:03

    Try this way to add push view controller.

    Create a table view controller and add it as the sub view of existing view controller. Then assign your table view and refresh controllers to tableview controller's properties.

    UITableViewController *newTableViewController = [[UITableViewController alloc] init];
    newTableViewControler.tableView = <yourTableView>;
    
    <yourRefreshController> = [[UIRefreshControl alloc] init];
    [<yourRefreshController> addTarget:self
                                action:@selector(refreshTableView:)
                      forControlEvents:UIControlEventValueChanged];
    
    newTableViewController.refreshControl = _chatListRefreshControl;
    [self addChildViewController:newTableViewController];
    
    0 讨论(0)
提交回复
热议问题