0

EDITED CODE.. Hello frds, i have one buttom in UIScrollview when i clicked on that button then i get btn.tag , so after that i want to scroll UITableView programatically. Here is my Code.

//-- Custom Scrollview--//
- (void)loadscrollViews
{
    int a =[DBConnection rowCountForTable:@"DD_Pic_Tags_Comments" where:[NSString stringWithFormat:@"img_Moment_id=%@",STR_Select_moment_id_for_timelinepage2]];
    numofimageinscrollview=a;
    int x=0;
    for (int i = 0; i < a; i++)
    {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(x, 0,scrollView.frame.size.height,scrollView.frame.size.height);
        btn.tag=i;
        UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.jpg",Pathforimage,[[Array_result objectAtIndex:i] objectForKey:@"img_File_Name"]]];
        [btn setImage:image forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(imageClick:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:btn];
        scrollView.contentSize=CGSizeMake((i+2)*scrollView.frame.size.height,scrollView.frame.size.height);
        x+=scrollView.frame.size.height+5;
    }
}

-(void)imageClick:(id)sender
{
    UIButton *btn=(UIButton *)sender;  //UIScrollView Button
    int count = (int)btn.tag;
    NSLog(@"click on small image in Scrollview>> count = %d",count);

    NSIndexPath *indexpath=[NSIndexPath indexPathForRow:count inSection:0];  //Set Count For RoW
    NSLog(@"%d %d",indexpath.row,indexpath.section);
    [self.Table reloadData];
    [self.Table scrollToRowAtIndexPath:indexpath
                             atScrollPosition:UITableViewScrollPositionTop
                                     animated:NO];
}

Get Error like,


2014-08-25 16:34:27.757 Omoment[5163:60b] click on small image in Scrollview>> count = 2
2014-08-25 16:34:27.758 Omoment[5163:60b] 2 0
2014-08-25 16:34:27.761 Omoment[5163:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (2) beyond bounds (0) for section (0).'
*** First throw call stack:
libc++abi.dylib: terminating with uncaught exception of type NSException

but, app crash , but when i set NSIndexPath *indexpath=[NSIndexPath indexPathForRow:1 inSection:0]; //at that time app does not crash, & there is no output in uitableview, & also scrollview did not scroll also..

  • You are adding the button to scrollview. Then from where does the tableview come into the scene? – iOS_DEV Sep 17 '14 at 06:39

2 Answers2

1

I think you have not defined the tag for that image in cellforrowatindexpath. I have used a button instead of image.I hope it works for u.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"CustomCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:simpleTableIdentifier owner:self options:nil] objectAtIndex:0];
    }

    cell.txtlabel.text = [NSString stringWithFormat:@"Hello %d", indexPath.row];
    [cell.btnnn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    cell.btnnn.tag=indexPath.row;
    return cell;
}

-(void)btnPressed:(id)sender{
    UIButton *btn=(UIButton *)sender;  //UIScrollView Button
    int count = (int)btn.tag;
    NSLog(@"click on small image in Scrollview>> count = %d",count);

    NSIndexPath *indexpath=[NSIndexPath indexPathForRow:count inSection:0];  //Set Count For RoW
    NSLog(@"%d %d",indexpath.row,indexpath.section);
   // [self.tbl reloadData];
    [self.tbl scrollToRowAtIndexPath:indexpath
                      atScrollPosition:UITableViewScrollPositionTop
                              animated:NO];
}
Kaps18
  • 156
  • 6
  • i have already do that, ... i set btn.tag in my UISrollView ...Here is my code.. - (void)loadscrollViews { int x=0; for (int i = 0; i < a; i++) { UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; btn.frame=CGRectMake(x, 0,scrollView.frame.size.height,scrollView.frame.size.height); btn.tag=i; // HERE I SET TAG // } } – mehul parmar iPhone Developer Aug 25 '14 at 12:55
0

You need to scroll the Scrollview.

For this first calculate the CGPoint up to which you need to scroll. Then use this:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

So your final code would be something like this:

-(void)btnPressed:(id)sender{
    UIButton *btn=(UIButton *)sender;  //UIScrollView Button

    CGFloat y_point = btn.center.y; // Adjust the y point according to you
    CGPoint thePoint = CGPointMake(0, y_point); // Assuming its a vertical scroll
    [self.myScroll setContentOffset:thePoint animated:YES];
}
iOS_DEV
  • 921
  • 6
  • 15