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

前端 未结 10 928
醉话见心
醉话见心 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:33

    It was my problem when I had a UISwitch as selector for the tables. But with the segmented control I haven't any problem. Maybe you didn't reload the table? This is my piece of working code:

    NSIndexPath *exPath = [[subTable indexPathsForVisibleRows] lastObject]; 
    
    isMatchOn = [whatList selectedSegmentIndex] == 0 ? YES : NO;  //table source will make the choice looking at this BOOL  
    
    [subTable reloadData];  // here tables actually flip
    
    if (lastPosition) {
        [subTable scrollToRowAtIndexPath:lastPosition atScrollPosition:UITableViewScrollPositionBottom animated:NO];   //I scroll to saved index
        }
    self.lastPosition = exPath;  //here I save the current position
    
    0 讨论(0)
  • 2020-12-08 03:36

    Did you try these 2 methods?

    They actually apply to the "scrolling" not just the offset of the content.

    [self.tableView  scrollToRowAtIndexPath:savedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    

    OR:

    [self.tableView  scrollRectToVisible:savedFrame animated:NO];
    

    They should actually effect the scrolling and by extension the acceleration of the table, not just what is visible on screen.

    0 讨论(0)
  • 2020-12-08 03:36

    You could wait for the 'decelerating' property to become NO (e.g. by using KVO) and switch after that.

    0 讨论(0)
  • 2020-12-08 03:39

    Have you tried using [view setContentOffset: offset animated: YES]?

    0 讨论(0)
  • 2020-12-08 03:41

    This worked nicely for me:

    if (self.tableView.isDecelerating) {
            NSArray *paths = [self.tableView indexPathsForVisibleRows];
            [self.tableView scrollToRowAtIndexPath:[paths objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
        }
    
    0 讨论(0)
  • 2020-12-08 03:47

    You could try doing

    tableView.scrollEnabled = NO;
    tableView.scrollEnabled = YES;
    

    This might stop the scroll by disabling it, then allow it again. I haven't tried this specifically, but I've done similar things.

    0 讨论(0)
提交回复
热议问题