2016-05-15 64 views
1

我想知道是否可以自动滚动触摸tableview?基本上,如果你有几个单元格,并且你正在滚动它们,并且你有一半的单元格在屏幕上,并且你触摸其中的一个单元格,我希望它滚动以便你触摸的单元格在屏幕上居中。原因是如果你有按钮,并且在单元格之间的中间滚动时他们触摸一个按钮,它将使单元格带有按钮到屏幕的中心,以便他们可以看到它。自动滚动到触摸tableviewcell

回答

1

我想你使用类似myArray.count的东西来定义numberOfRowsInSection方法中的单元格数量。所以,如果你想滚动到中间位置的单元格,你可以使用myArray.count/2找到该行的数量。

NSIndexPath* ip = [NSIndexPath indexPathForRow:myArray.count/2 inSection:0]; 
[self.myTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

斯威夫特版本:

var ip: NSIndexPath = NSIndexPath(forRow: myArray.count/2, inSection: 0) 
self.myTable.scrollToRowAtIndexPath(ip, atScrollPosition: .Top, animated: true) 

希望帮助!

+0

任何机会,你可以进行转换的迅速? –

+0

看我的编辑,我用这个页面来说明:[link](https://objectivec2swift.com/#/home/converter/) –

1

玛丽亚的回答转化为斯威夫特:

var ip = NSIndexPath(forRow: myArray.count/2, inSection: 0) 
self.myTable.scrollToRowAtIndexPath(ip, atScrollPosition: .Top, animated: true)