Programmatically force a UIScrollView to stop scrolling, for sharing a table view with multiple data sources

前端 未结 10 992
醉话见心
醉话见心 2020-12-08 03:04

I have a UITableView whose data source and delegate are switched between a couple of custom data source objects when the user touches a segmented control (think \"Top Paid\"

10条回答
  •  情书的邮戳
    2020-12-08 03:49

    I used Corey's approach. I save & restore the rects with the dictionary representation. Also, it may not be obvious but the rect to preserve & restore is the bounds of the UITableView:

    // Save the current tableview bounds
    CGRect contentRect = self.listTableView.bounds;
    if (!!oldScope) [_contentRects setObject:(NSObject *)CGRectCreateDictionaryRepresentation(contentRect) forKey:oldScope];
    
    // Restore if possible
    CFDictionaryRef restoredFrameDict = (CFDictionaryRef)[_contentRects objectForKey:newScope];
    if (!restoredFrameDict) restoredFrameDict = CGRectCreateDictionaryRepresentation(CGRectZero);
    CGRectMakeWithDictionaryRepresentation(restoredFrameDict, &contentRect);
    
    // Switch over to new datasource
    self.listTableView.dataSource = [self dataSourceForScope:newScope];
    [self.listTableView reloadData];
    
    // Restore content offset for "newScope"; Also stops scrolling
    [self.listTableView scrollRectToVisible:contentRect animated:NO];
    

    Note the interchangeable use of CFDictionaryRef and NSDictionary *

提交回复
热议问题