Detecting UITableView scrolling

后端 未结 3 1317
予麋鹿
予麋鹿 2020-12-13 17:04

I\'ve subclassed UITableView (as KRTableView) and implemented the four touch-based methods (touchesBegan, touchesEnded, touchesMoved, and touchesCancelled) so that I can det

相关标签:
3条回答
  • 2020-12-13 17:29

    You don't need to intercept the event methods. Check the docs for the UIScrollViewDelegate protocol, and implement the -scrollViewDidScroll: or -scrollViewWillBeginDragging: methods as appropriate to your situation.

    0 讨论(0)
  • 2020-12-13 17:44

    I would like to add the following:

    If you are working with a UITableView it is likely that you are already implementing the UITableViewDelegate to fill the table with data.

    The protocol UITableViewDelegate conforms to UIScrollViewDelegate, so all you need to do is to implement the methods -scrollViewWillBeginDragging and -scrollViewDidScroll directly in your UITableViewDelegate implementation and they will be called automatically if the implementation class is set as delegate to your UITableView.

    If you also want to intercept clicks in your table and not only dragging and scrolling, you can extend and implement your own UITableViewCell and use the touchesBegan: methods in there. By combining these two methods you should be able to do most of the things you need when the user interacts with the UITableView.

    0 讨论(0)
  • 2020-12-13 17:46

    It was my mistake, I tried to call the method before reloading the tableview. The following code helped me solve this issue.

    [mytableview reloadData];

    [mytableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];

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