简体   繁体   中英

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

I could not figure out why the scroll view is not being scrolled whereas the current_set variable is getting increased.What is the exact position to write the code to scroll the UIScrollView when it is a subview of the UITableView?

Here is my code:

The definition for method: 的定义方法:

- (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;
}

Here is the definition for method 的定义方法

-(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;
 }
}

The method which is actually scrolling the scroll view

-(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];
}

Instead of using the 'scrollRectToVisible' method, you may try to set 'contentOffset' of the ScrollView.

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

x is the point you want the scrollview scroll to. If you set to (0,0) which is the front.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM