在非弹跳UITableView中滚动后,行选择不会在第一次点击时起作用

时间:2015-03-19 09:09:51

标签: ios objective-c uitableview uiscrollview

基本上我看到的问题看起来像苹果虫。问题是在滚动表格之后,任何行上的第一次点击只会突出显示它。需要第二次点击才能实际选择或取消选择它。我注意到这个问题大多数时间都会发生。它很少会按预期工作,但我没有注意到它何时起作用的模式。

只有theTableView.bounces = NO;才会出现此问题,否则它会完美运行。

我通过实施适当的委托方法验证了这一点。

滚动后首先点按任意一行,我会收到这些回电

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath

滚动后在相同或不同的行上进行后续点击,我得到这些回调

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath

//then

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//or
-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

我见过其他类似的问题,但是所有这些问题都在另一个滚动视图中使用了一个表,这不是这里的情况。

是否有人为此找到了修复或解决方法?我试过iOS 7.0 ... 8.2并且所有这些问题都存在。

谢谢!

2 个答案:

答案 0 :(得分:9)

经过大量测试后,我发现问题发生在表contentOffset.y达到最大值或0时。因此我创建了自己的“弹跳效果”,但实施{{1}的规模要小得多在表视图的委托中如下:

scrollViewWillEndDragging:withVelocity:targetContentOffset:

有了这个,问题就消失了。这是一种解决方法,但就像一个魅力!

答案 1 :(得分:2)

this comment的Swift 4语法:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    targetContentOffset.pointee.y = max(targetContentOffset.pointee.y - 1, 1)
}