iOS:如何判断是否在UITableView中滚动通过某一行?
我需要一种方法来检测我是否在UITableView中滚动过第5行。一旦我通过第五行,我想调用一个方法。我不知道怎么做,也不知道从哪里开始。我将发布UITableView代码。如果有人能帮助我,我将不胜感激iOS:如何判断是否在UITableView中滚动通过某一行?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我需要一种方法来检测我是否在UITableView中滚动过第5行。一旦我通过第五行,我想调用一个方法。我不知道怎么做,也不知道从哪里开始。我将发布UITableView代码。如果有人能帮助我,我将不胜感激 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ListingCell *cell ; if (cell == nil) {
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ListingCell *cell ;
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ListingCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 100;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
您所要做的就是实现UIScrollViewDelegate方法
-scrollViewDidScroll
。然后,您可以从当前UITableViewcontentOffset
属性中手动计算所需的任何内容
-(void)scrollViewDidScroll:(UIScrollView *)inScrollView
{
int currentOffsetY = inScrollView.contentOffset.y;
//Now use that y position to figure out if you have scrolled past where you want.
}
您可能还需要一个布尔值,因此只需调用“pass here”方法一次。您所要做的就是实现UIScrollViewDelegate方法
-scrollViewDidScroll
。然后,您可以从当前UITableViewcontentOffset
属性中手动计算所需的任何内容
-(void)scrollViewDidScroll:(UIScrollView *)inScrollView
{
int currentOffsetY = inScrollView.contentOffset.y;
//Now use that y position to figure out if you have scrolled past where you want.
}
您可能还需要布尔值,因此只需调用“pass here”方法一次。请尝试以下操作:
- (void)scrollViewDidScroll:(UITableView *)tableView
{
CGRect row = [tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0];
if (tableView.contentOffset.y > CGRectGetMaxY(row)) {
// you did it
}
}
或CGRectGetMinY
,具体取决于您想要实现的目标。请尝试以下操作:
- (void)scrollViewDidScroll:(UITableView *)tableView
{
CGRect row = [tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0];
if (tableView.contentOffset.y > CGRectGetMaxY(row)) {
// you did it
}
}
或
CGRectGetMinY
,具体取决于您试图实现的目标。如果您的所有行的高度都相同,则实现下面Putz1103的答案应该足够简单。如果您的所有行的高度都相同,则实现下面Putz1103的答案应该足够简单。