UIImageView がプログラムで UITableView をトラバースするアプリを構築しています。UIImageView の移動方向を制御する 2 つのボタン (「上」と「下」) を作成しました。私がやろうとしているのは、UIImageView が現在表示されていない行に上下に移動している場合、プログラムで UITableView をスクロールすることです。セルが返された配列内にあるかどうかを確認するには、UITableViewメソッド「visibleCells」を使用する必要があることに気付きました。セルが配列にない場合は、テーブルを正確に 1 行ずつ上下にスクロールする必要があります (ユーザーが「上」/「下」ボタンをクリックするたびに)。残念ながら、このメソッドの使用方法に関する良い例に出くわしたことがなく、助けが必要です. ここに私が持っているコードがあります:
- (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);
}];
}
}
私が達成しようとしている機能を達成する方法を誰かに教えてもらえますか?