how to implement cyclic scrolling on tableview

前端 未结 4 734
走了就别回头了
走了就别回头了 2021-01-04 21:26

Could you please help me with circular scrolling in tableview please.

I want that if I scroll down tableview, the rows should go in the reverse way -- it should app

4条回答
  •  [愿得一人]
    2021-01-04 21:56

    You could "fake" the cyclic scrolling repeating the same cells all over again. In the numberOfRowsInSection method, return n times the actual number of rows. Make sure n is big enough.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return numberOfActualRows*100;
    }
    

    Then in the cellForRowAtIndexPath method (and elsewhere) use the mod operator (%) to return the proper cell.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSUInteger actualRow = indexPath.row % numberOfActualRows;
        ...
    }
    

    You may want to hide the sroll indicator.

    self.tableView.showsVerticalScrollIndicator = NO;
    

    You may also want to scoll the table view to the middle before you display the table so scrolling backwards works fine.

    [self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]/2 inSection:0]  atScrollPosition:UITableViewScrollPositionTop animated:NO];
    

    Of course, the user would eventually hit the bottom or the top if he/she kept scrolling over and over.

提交回复
热议问题