开发者

Section Headers in UITableView when inset of tableview is changed

I have implemented a "Pull to refresh" in my tableView like the i开发者_如何学JAVAPhone app Twitter or Facebook.

My tableView has sections with head views. When the tableView is in "Refresh mode", so when I pulled the tableView to refresh, I set the contentInset of the tableView to display the tableView a certain way. At this moment, if I push the tableView up, the headers of the UITableView are not anymore scrolling to the top of the UITableView. See the following screenshots:

Section Headers in UITableView when inset of tableview is changed

Section Headers in UITableView when inset of tableview is changed

How can I fix that to make the headers scroll like expected?

Thanks!


I had the same problem when using contentInset and fixed it by adding code to scrollViewDidScroll: to update the contentInset dynamically while in the "loading" state.

if( refreshState == kLoading ) {
    if( scrollView.contentOffset.y >= 0 )
        scrollView.contentInset = UIEdgeInsetsZero;
    else
        scrollView.contentInset = UIEdgeInsetsMake( MIN( -scrollView.contentOffset.y, kPullHeight ), 0, 0, 0 );
}       


When doing this, I usually use the table-wide header in UITableView instead:

@property(nonatomic, retain) UIView *tableHeaderView

...instead of contentInsets. There were a lot of bugs with contentInsets all the way back to iOS 2.x days, and I'm not sure they've all been fixed. Is there a reason you can't use a table header?


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];
    


Looks like an UIKit bug. Found a workaround:

// your initial inset
let kInset = 40.0

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.y
    if (offset < 0) {
        scrollView.contentInset = UIEdgeInsets(top: kInset, left: 0.0, bottom: 0.0, right: 0.0)
    } else {
        scrollView.contentInset = .zero
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜