我正在构建一个应用程序,其中UIImageView以编程方式遍历UITableView。我创建了两个按钮(“向上”和“向下”)来控制UIImageView移动的方向。我想要做的是以编程方式滚动UITableView,如果UIImageView向上/向下移动到目前不可见的行。我意识到我需要使用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);
}];
}
}
有人能告诉我如何完成我想要实现的功能吗?
答案 0 :(得分:0)
如果您有一点时间学习新内容,我建议您尝试使用iCarousel @nick-lockwood。 Yo可以使用垂直iCarousel来实现这样的目标:
它类似于UITableView,但对于旋转木马中的每个项目,您可以设置任何UIView,然后您可以显示设置currentItemIndex
属性的特定项目!
看看它是否适合你。 iCarousel救了我两天的工作!