I want to scroll to a row in a table view, select the cell then deselect it, but its being deselected before i see it
im using code as below
[sampleListTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[sampleListTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[sampleListTableView deselectRowAtIndexPath:indexPath animated:YES];
the behaviour i am looking for is that the table view will scroll to the path (which it does) then select the cell momentarily (which it does, if the cell is on the screen)
im thinking that the scroll animation is threaded and the cell is being selected before it appears on the screen, this is confirmed in a way as when i call this code with the cell on the screen it selects and then deselects.
anyone have a workaround? i can do it this way (without the scroll animation) but its not as sweet, very abrupt in fact
开发者_JAVA百科 [sampleListTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
[sampleListTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[sampleListTableView deselectRowAtIndexPath:indexPath animated:YES];
I'm not sure but you can try to implement some delegate methods of UIScrollViewDelegate protocol. smth like that
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
and make deselection there.
Here's code from one of my projects:
#define kHighlightTableRowDuration 0.5
- (void)scrollToLastInsertedItem:(NSIndexPath *)indexPath
{
@try
{
[myTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
if( rows >= 8 )
[myTableView performSelector:@selector(flashRowAtIndexPath:) withObject:indexPath afterDelay:0.5];
}
@catch(NSException *ex) // NSRangeException
{
ALog(@"NSRangeException: %@",ex);
}
}
// briefly highlight the last added item
- (void)flashRowAtIndexPath:(NSIndexPath *)indexPath
{
// if cell is offscreen, nil returned
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
if( cell ){
BOOL shouldHighlight = YES;
if( ! cell.highlighted ){
DLog(@"Starting cell highlight, setting on: %@", indexPath);
shouldHighlight = YES;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[self performSelector:@selector(flashRowAtIndexPath:) withObject:indexPath afterDelay:kHighlightTableRowDuration];
}else{
DLog(@"Ending cell highlight, setting off: %@", indexPath);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
shouldHighlight = NO;
}
[cell setHighlighted:shouldHighlight animated:YES];
}
}
flashRowAtIndexPath
is called twice, the first time to turn highlight on, then again to turn it off after given delay. In my UITableView
selection style is normally "none", so I have to change the selection style to get the highlighting to happen.
精彩评论