【问题标题】:How to scroll UIScrollView programmatically which is a subview of the custom UITableViewCell?如何以编程方式滚动 UIScrollView,这是自定义 UITableViewCell 的子视图?
【发布时间】:2016-04-15 21:18:36
【问题描述】:

我无法弄清楚为什么滚动视图没有被滚动,而 current_set 变量正在增加。当 UIScrollView 是 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];
}

【问题讨论】:

  • 我会使用您在“willDisplayCell:forRowAtIndexPath:”中的代码并将其与“cellForIndexPath”一起放入可能会尝试一下

标签: ios objective-c uitableview uiscrollview


【解决方案1】:

您可以尝试设置 ScrollView 的 'contentOffset',而不是使用 'scrollRectToVisible' 方法。

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

x 是您希望滚动视图滚动到的点。如果设置为 (0,0) 即为最前面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多