0

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

Shymaa Othman
  • 239
  • 1
  • 3
  • 16
  • I think you need to initialize `paged` (not just `alloc` it). Also, set breakpoints and see if both `paged` and `page` are what you want them to be. – Cashew Sep 27 '12 at 02:15
  • thanks I tried to initiate paged but no change happened , page returns a right number but what do you mean with check - paged - what is expected ?! – Shymaa Othman Sep 27 '12 at 02:28
  • oh, wait, i think I got it. You are creating a new `PagedScrollViewController` instance, but are you adding it (as a subview) anywhere? What do you mean by "ScrollView page do not changed when back from tableview" ? It's because you're changing the slider value of the NEW `PagedScrollViewController` that you just created, but NOT the `PagedScrollViewController` that's already there. – Cashew Sep 27 '12 at 02:38
  • also, the reason `pnt` returns negative values is that `paged.scrollView.frame` is probably not set. – Cashew Sep 27 '12 at 02:53
  • I do not need to add any subview , all what I need when choosing row cell with text "photo no 5" to return to PagedScrollViewController and set scrollview content to picture no 5 . How I can do that ?! – Shymaa Othman Sep 27 '12 at 02:53
  • Is your `PagedScrollViewController` created in the XIB file or in code? if in XIB, create an outlet for it and use it to change the slider values etc. – Cashew Sep 27 '12 at 02:54
  • ok , but Is it needed to be connected as an outlet ? connected with what ?? BTW I am using storyboard and PagedScrollViewController is a UIViewController . – Shymaa Othman Sep 27 '12 at 03:11
  • ok, let's start at the beginning. Is your `PagedScrollViewController` the root view controller? and so is `didSelectRowAtIndexPath` inside the `PagedScrollViewController.m` file? – Cashew Sep 27 '12 at 03:13
  • didSelectRowAtIndexPath is in another TableViewController which contains favorites list of pictures . – Shymaa Othman Sep 27 '12 at 03:22
  • this is my storyboard http://imageshack.us/f/204/screenshotatq.png/ – Shymaa Othman Sep 27 '12 at 03:28

1 Answers1

0

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).

Cashew
  • 1,466
  • 2
  • 16
  • 21
  • will PageScrollViewController outlet need to be connected ? connected with what ?! – Shymaa Othman Sep 27 '12 at 03:34
  • I did that but nothing changed ): – Shymaa Othman Sep 27 '12 at 03:41
  • do you have your outlet set? That is, does `myPaged` point to your PageScrollViewController? – Cashew Sep 27 '12 at 03:44
  • search google for creating outlets from the xib file. Should be pretty straightforward (and essential for most apps)... – Cashew Sep 27 '12 at 03:51
  • how could my paged which is an outlet in another controller can point out to PageScrollViewController ?! I cannot get you . – Shymaa Othman Sep 27 '12 at 03:57
  • don't use paged. forget it. Instead, create an outlet called myPaged that points to PageScrollViewController. Use myPaged in your code to change the slider values. – Cashew Sep 27 '12 at 04:01
  • I already did that and nothing changed .. so I asked Is the new outlet need to be connected with any thing ?! – Shymaa Othman Sep 27 '12 at 04:06
  • the outlet variable (called myPaged) has to be connected to the PageScrollViewController in the xib. (check google on how to do it) – Cashew Sep 27 '12 at 04:33