简体   繁体   中英

Trying to use visibleCells when scrolling UITableView in iOS

I am building an app where a UIImageView traverses a UITableView programmatically. I have created two buttons ("up" and "down") that control which direction the UIImageView moves. What I am trying to do is programmatically scroll the UITableView if the UIImageView is moving up/down to a row that is not presently visible. I realize I need to use the UITableView method, "visibleCells" to check to see if the cell is within the array is returned. If the cell is not in the array, then I need to scroll the table up/down by precisely one row (each time the user clicks on the "up"/"down" button). Unfortunately I have not come across any good examples on how to use the method, and I need help. Here is the code that I have here:

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

    }

}

Can anyone show me how to accomplish the functionality that I am trying to achieve?

If you have a little bit time to learn something new, i recommend you to try with iCarousel by @nick-lockwood . Yo can use a vertical iCarousel to achieve something like that:

垂直iCarousel

It's like a UITableView, but for each item in the carousel you can set any UIView, and then you can show a specific item setting the currentItemIndex property!

Take a look if it fits to you. iCarousel saved me two days of work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM