簡體   English   中英

如何以編程方式滾動UIScrollView,它是自定義UITableViewCell的子視圖?

[英]How to scroll UIScrollView programmatically which is a subview of the custom UITableViewCell?

我不知道為什么在增大current_set變量的情況下不能滾動滾動視圖。當它是UITableView的子視圖時,編寫代碼以滾動UIScrollView的確切位置是什么?

這是我的代碼:

tableView:cellForRowAtIndexPath:的定義方法:

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *cellIdentifier=[NSString stringWithFormat:@"regularExerciseCell%li",indexPath.row];
   RegularExerciseCell *cell=[tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];
   if(!cell)
   {
      [tableView1 registerNib:[UINib nibWithNibName:@"RegularExerciseCell" bundle:nil] forCellReuseIdentifier:@"regularExerciseCell"];
      cell=[tableView1 dequeueReusableCellWithIdentifier:@"regularExerciseCell"];
   }
   NSLog(@"cellForRow At indexPath %li",indexPath.row);
   return cell;
}

這是tableView的定義:willDisplayCell:forRowAtIndexPath:方法

-(void)tableView:(UITableView *)tableView willDisplayCell:(RegularExerciseCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(selectedIndex==indexPath.row)
  {
    cell.routineViewCard.hidden=NO;
    //SCROLL VIEW
    float scrollView_width=[UIScreen mainScreen].bounds.size.width;
    cell.setsScrollView.tag=indexPath.row;
    totalSetsInActiveExercise=[array count];
    for (int k=0; k<=totalSetsInActiveExercise; k++)
    {
      [cell.setsScrollView addSubview:[self subviewOfScrollView:scrollView_width]];
    }
    cell.setsScrollView.contentSize = CGSizeMake(scrollView_width*([workoutViewSetData count]+1),cell.setsScrollView.frame.size.height);
    if(condition) //this condition may be true or false depending upon the scenario
    {
      [self moveToNextSet:indexPath.row  and:@"left"];
    }
  }
  else
  {
    cell.routineViewCard.hidden=YES;
 }
}

實際滾動滾動視圖的方法

-(void)moveToNextSet:(long)sender_tag  and:(NSString*)direction
{
  NSIndexPath *indexPath=[NSIndexPath indexPathForItem:sender_tag inSection:1];
  RegularExerciseCell *cell=(RegularExerciseCell*) [workoutTableView cellForRowAtIndexPath:indexPath];
  if ([direction isEqualToString:@"right"])
  {
    if(current_set!=0)
        current_set--;
  }
  else if([direction isEqualToString:@"left"])
  {
    current_set++;
  }

  CGRect frame = CGRectMake((cell.setsScrollView.bounds.size.width*(current_set)),0,cell.setsScrollView.bounds.size.width,cell.setsScrollView.bounds.size.height);
 [cell.setsScrollView scrollRectToVisible:frame animated:YES];
}

您可以嘗試設置ScrollView的“ contentOffset”,而不是使用“ scrollRectToVisible”方法。

[cell.setsScrollView setContentOffset:CGPointMake(x, 0) animated:true];

x是您希望滾動視圖滾動到的點。 如果您將(0,0)設置為最前面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM