繁体   English   中英

UITableView在框架中间选择的行

[英]UITableView selected row in the middle of frame

我需要将所选的行放在框架的中间。 因此,如果我选择第一行或最后一行,则此行需要位于中间,而不是剪切到表格视图的顶部(或底部)(类似于UIPickerView )。

这是我在scrollViewWillEndDragging代码:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                     withVelocity:(CGPoint)velocity
              targetContentOffset:(inout CGPoint *)targetContentOffset {

    CGFloat rowHeight = [delegate rowHeightForMyPickerView:self];
    CGFloat floatVal = targetContentOffset->y / rowHeight;
    NSInteger rounded = (NSInteger)(lround(floatVal));
    targetContentOffset->y = rounded * rowHeight;
    NSLog(@"Offset: %f Float: %f Row: %d",targetContentOffset->y,floatVal,rounded);
    [delegate myPickerView:self isOverRow:rounded];
}

使用此代码,我创建了捕捉效果,但rounded不是正确的行。 将第一个和最后一个像元高度更改为双倍高度会使问题消失,但是我不喜欢这种解决方案。

UITableView类还可以使用scrollToRowAtIndexPath:atScrollPosition:animated方法滚动到选定的indexPath以及scrollToRowAtIndexPath:atScrollPosition:animated 最好在tableView:didSelectRowAtIndexPath:indexPath方法中,添加:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; //scrolls the selected cell to middle
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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