Search code examples
iosscrollviewtableviewuislidercgpoint

iOS -Setting ScrollView Current Page


I am working on a project in which I need a scrollview to show a number of images and I allow user to set a definite picture from scrollview as a favorite one and save favorites in tableview In order to get it again as user wish .

My problem is concerning setting scrollview current page by a value returned from selecting a row cell in tableview

Now this is my code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *str = cell.textLabel.text;

    PagedScrollViewController *paged = [PagedScrollViewController alloc];

    NSString *page_str = [str substringFromIndex:9];

    int page_number = [page_str intValue];

    [paged.slider setValue: page_number] ;

    [paged.current_page_lbl setText:[[NSString alloc]initWithFormat:@"%i",page_number ]];

    int page = paged.slider.value ;

    NSLog(@"go to page %d",page_number);

    CGPoint pnt = CGPointMake((page-1) * paged.scrollView.frame.size.width , 0) ;
    [paged.scrollView setContentOffset:pnt animated:YES];

    NSLog(@"point is %d",pnt);

    [self dismissModalViewControllerAnimated:YES];

}

ScrollView page do not changed when back from tableview

also CGPoint point returned a negative value do not changed ever ! What is the problem with that ?!

===========

I still need help :

how to update the scrollview when returning from tableview with a definite page number ?!

this is my storyboard


Solution

  • Inside your TableViewController, create an outlet to your PageScrollViewController (the one in XIB). Call the outlet myPaged or something.

    Then, inside didSelectRowAtIndexPath, you can use myPaged instead of paged. (take out the line where you are creating a new instance of PageScrollViewController).

    for example:

    [myPaged.slider setValue: page_number];
    

    will change the the slider of your PageScrollViewController in the XIB file (the one you want).