Uitableview 使用自定义单元格以编程方式滚动tableview

Uitableview 使用自定义单元格以编程方式滚动tableview,uitableview,ios7,scroll,custom-cell,Uitableview,Ios7,Scroll,Custom Cell,编辑的代码。。 你好,frds,当我单击UIScrollview中的一个按钮时,我在该按钮上有一个按钮,然后我得到btn.tag,所以在此之后,我想以编程方式滚动UITableView。 这是我的代码。 //-- Custom Scrollview--// - (void)loadscrollViews { int a =[DBConnection rowCountForTable:@"DD_Pic_Tags_Comments" where:[NSString stringWithFor

编辑的代码。。 你好,frds,当我单击UIScrollview中的一个按钮时,我在该按钮上有一个按钮,然后我得到btn.tag,所以在此之后,我想以编程方式滚动UITableView。 这是我的代码。

//-- 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];
}
但是,, 应用程序崩溃,但当我设置nsindepath*indepath=[nsindepath indexPathForRow:1第0节]时;
//当时应用程序没有崩溃,&uitableview中没有输出,&scrollview也没有滚动..

我想您还没有在CellForRowatineXpath中定义该图像的标记。我用了一个按钮而不是图片。我希望它对你有用

- (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];
}

您需要滚动滚动滚动视图

为此,首先计算需要滚动到的CG点。 然后用这个:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
因此,您的最终代码如下所示:

-(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];
}

我已经这么做了。。。我在UIScrollView中设置了btn.tag…这是我的代码..-(void)loadscrollViews{int x=0;for(int i=0;i-(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]; }