开发者

ScrollView issue...need to jump to exact location

开发者 https://www.devze.com 2023-03-14 22:01 出处:网络
Scrollviewcustomcontrol not sending correct ID to display the desired output. We have two textviews showing 4 articles snippets in scrollview. They are subviews of scrollview. Now clicking on them, I

Scrollviewcustomcontrol not sending correct ID to display the desired output. We have two textviews showing 4 articles snippets in scrollview. They are subviews of scrollview. Now clicking on them, I wish to open a details page of that article along with scroller so that all news could be browsed/scrolled in full form. ScrollerView is in place, working with paging, and showing all the records from beginning. Its just not going to the exact article that we clicked. I tried to pass values via textviews and buttons assigned to it. But it can just pass two values since we have two textviews..and there are four articles..article number 3 is passing the ID of article number 1. And article number 4 is passing the ID of article number 2. Any ideas?

    - (IBAction) tappedItemAtIndex:(id)sender {

UIButton *tappedButton = (UIButton*) sender;
NSInteger selectedIndex = [tappedButton tag];

}

    int j=0;
    for (int i = 0; i < rowOne.count; i++) {

        if (j==1) {

            j++;


        }

        count =i+1;
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    UIView*subView=[[UIView alloc ]initWithFrame:frame];
    [self.scrollView addSubview:subView];

    CGRect myframe=CGRectMake(107, 46, 258, 128);        

    //CGRect myframe=CGRectMake(13, 46, 352, 128);
    Book*book=[rowOne objectAtIndex:i];
    UIButton*backViewButton=[UIButton buttonWithType:UIButtonTypeCustom];

     backViewButton.frame=CGRectMake(107, 46, 258, 128);    
    [backViewButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];



        buttontitle=book.title;

        [backViewButton addTarget:self action:@selector(tappedItemAtIndex:)forControlEvents:UIControlEventTouchUpInside];    
        [backViewButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backViewButton setTitle:buttontitle forState:UIControlStateNormal];
        [subView addSubview:backViewButton];


        backViewButton.tag=j;


        j++;

        Book*book1=[rowOneSecond objectAtIndex:i];
        UIButton*backViewButton1=[UIButton buttonWithType:UIButtonTypeCustom];

        backViewButton1.frame=CGRectMake(531, 99, 300开发者_StackOverflow, 100);    
        [backViewButton1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        NSString*title=book1.title;

        backViewButton1.tag=j;

        [backViewButton1 addTarget:self action:@selector(tappedItemAtIndex:)forControlEvents:UIControlEventTouchUpInside];    
        [backViewButton1 setTitle:title forState:UIControlStateNormal];
        [subView addSubview:backViewButton1];                                               

} 


Did you try setting tag to the textViews. Like when you add them in scrollview I assume you run a loop so add textView to a button and then add that button to scrollview and assign tag = index(loop increment variable). Add target to button. From there you can get the tag of button and then you pass it to super class by creating a delegate protocol.

Update

//I assume you have this type of function bound to each button's touchUpInside.

- (IBAction) tappedItemAtIndex:(id)sender {
    UIButton *tappedButton = (UIButton*) sender;
    NSInteger selectedIndex = [tappedButton tag];

}


To scroll to the content based on pages, store a pageNumber in your scrollerview. When you press "next page" button or somehow scroll to the next page, increment the page number and then every time you are loading a page call:

- (void)loadPage:(NSInteger)pageNumber
{
    // textViews is an NSArray of the 2 textviews you have in your scroller view
    // articles is an NSArray in scrollerview containing all articles as strings
    unsigned int i, article;
    for(i = 0; i < [textViews count]; ++i)
    {
        article = pageNumber*[textViews count]+i;
        if(article < [articles count])
        {
            [(UITextView *)[textViews objectAtIndex:i]
             setText:[articles objectAtIndex:article]];
        } else {
            [(UITextView *)[textViews objectAtIndex:i]
             setText:@""]; // blank pages, because we're at the end
        }
    }
}


There is property of UIScrollView contentOffSet,

CGPointMake which is the exact point you need to jump

scrollView.contentOffSet = CGPointMake();

its helpful for you, i had implement earlier..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号