2016-04-15 39 views
0

我無法弄清楚爲什麼滾動視圖沒有被滾動,而current_set變量越來越多。當它是UITableView的子視圖時,寫入代碼以滾動UIScrollView的確切位置是什麼?如何以編程方式滾動UIScrollView,這是自定義UITableViewCell的子視圖?

這裏是我的代碼:

的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]; 
} 
+0

我會使用'willDisplayCell:forRowAtIndexPath:'中的代碼,並將它與'cellForIndexPath'放在一起可能會給你一個嘗試 – ErickES7

回答

0

而不是使用「scrollRectToVisible」方法,您可以嘗試設置了滾動的「contentOffset」。

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

x是您希望scrollview滾動到的點。如果你設置爲(0,0)這是前面。

相關問題