【问题标题】:scroll tableview programatically with custom cell使用自定义单元格以编程方式滚动 uitableview
【发布时间】:2014-08-25 11:13:48
【问题描述】:

编辑代码.. 你好 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];
}

得到类似的错误,


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

但是, 应用程序崩溃,但是当我设置 NSIndexPath *indexpath=[NSIndexPath indexPathForRow:1 inSection:0]; //当时app没有crash,&uitableview没有输出,&scrollview也没有滚动..

【问题讨论】:

  • 您正在将按钮添加到滚动视图。那么tableview是从哪里来的呢?

标签: uitableview ios7 scroll custom-cell


【解决方案1】:

我认为您尚未在 cellforrowatindexpath 中为该图像定义标签。我使用了一个按钮而不是图像。我希望它对你有用。

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

【讨论】:

  • 我已经这样做了,...我在 UISrollView 中设置了 btn.tag ...这是我的代码.. - (void)loadscrollViews { int x=0; for (int i = 0; i
【解决方案2】:

您需要滚动滚动视图。

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

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

【讨论】:

    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多