0

我正在构建一个应用程序,其中 UIImageView 以编程方式遍历 UITableView。我创建了两个按钮(“向上”和“向下”)来控制 UIImageView 移动的方向。如果 UIImageView 向上/向下移动到当前不可见的行,我想要做的是以编程方式滚动 UITableView。我意识到我需要使用 UITableView 方法“visibleCells”来检查单元格是否在返回的数组中。如果单元格不在数组中,那么我需要将表格向上/向下滚动精确一行(每次用户单击“向上”/“向下”按钮时)。不幸的是,我没有遇到任何关于如何使用该方法的好例子,我需要帮助。这是我在这里的代码:

- (IBAction)buttonClicked:(id)sender {

    if([sender tag] == 1){

        //here is where I will need to make the call to [_tableView visibleCells] to see if the cell that the UIImageView is about to scroll up to is within this array.  If not, then programmatically scroll up one row.
        if (_index.row == 0) {

            _index = [NSIndexPath indexPathForRow:[_tableData count] - 1 inSection:_index.section];
            [_table scrollToRowAtIndexPath:_index atScrollPosition:UITableViewScrollPositionTop animated:YES];

        }

        else
            _index = [NSIndexPath indexPathForRow:_index.row - 1 inSection:_index.section];

        [UIView animateWithDuration:.3 animations:^{
            CGRect rect = [self.view convertRect:[_table rectForRowAtIndexPath:_index] fromView:_table];

            CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
            _imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
        }];

    }

    else if([sender tag] == 2){


         if (_index.row + 1 == [_tableData count]) {

            _index = [NSIndexPath indexPathForRow:0 inSection:_index.section];
            [_table scrollToRowAtIndexPath:_index atScrollPosition:UITableViewScrollPositionBottom animated:YES];

        }

         else
             _index = [NSIndexPath indexPathForRow:_index.row + 1 inSection:_index.section];

        [UIView animateWithDuration:.3 animations:^{
            CGRect rect = [self.view convertRect:[_table rectForRowAtIndexPath:_index] fromView:_table];

            CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
            _imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
        }];

    }

}

谁能告诉我如何完成我想要实现的功能?

4

1 回答 1

0

如果您有一点时间来学习新东西,我建议您尝试使用@ nick-lockwood的iCarousel。哟可以使用垂直 iCarousel 来实现这样的事情:

垂直 iCarousel

它就像一个 UITableView,但是对于轮播中的每个项目,您可以设置任何 UIView,然后您可以显示设置currentItemIndex属性的特定项目!

看看它是否适合你。iCarousel 为我节省了两天的工作时间!

于 2013-06-12T01:46:09.210 回答