我该如何将表格的单元格滚动到特定位置?我有一个表格,显示3行(根据高度)。我想做的是,如果我点击第一行,根据表格的高度,第一行应该滚动并获得新位置(中心),其他行也一样。我尝试了contenOffset但没有成功。
编辑:
简而言之,就像数据选择器一样,当我们选择选择器中的任何行时,该行会滚动到中心。
谢谢..
我该如何将表格的单元格滚动到特定位置?我有一个表格,显示3行(根据高度)。我想做的是,如果我点击第一行,根据表格的高度,第一行应该滚动并获得新位置(中心),其他行也一样。我尝试了contenOffset但没有成功。
编辑:
简而言之,就像数据选择器一样,当我们选择选择器中的任何行时,该行会滚动到中心。
谢谢..
使用- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
方法应该可以解决问题,用法如下:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[yourTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
atScrollPosition
可以取以下任意值:
typedef enum {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
} UITableViewScrollPosition;
UITableViewDelegate
中的方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
将被调用,因此在该方法中调用scrollToRowAtIndexPath
。我强烈建议您阅读iOS表视图编程指南
以及UITableView
参考和UITableViewDelegate
参考,您会发现它们非常有用,毕竟,这就是文档的用途。 - Fran SevillanoSwift 4.2版本:
let indexPath:IndexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .none, animated: true)
枚举: 这是可用的tableView滚动位置,仅供参考。您无需在代码中包含此部分。
public enum UITableViewScrollPosition : Int {
case None
case Top
case Middle
case Bottom
}
DidSelectRow:
->DidSelectRow:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let theCell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
if let theCell = theCell {
var tableViewCenter:CGPoint = tableView.contentOffset
tableViewCenter.y += tableView.frame.size.height/2
tableView.contentOffset = CGPointMake(0, theCell.center.y-65)
tableView.reloadData()
}
}
[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
这将把您的表格视图滚动到第一行。
最后我发现......当表格仅显示3行时,它可以很好地工作......如果行数更多,则需要相应地进行更改......
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text =[NSString stringWithFormat:@"Hello roe no. %d",[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * theCell = (UITableViewCell *)[tableView
cellForRowAtIndexPath:indexPath];
CGPoint tableViewCenter = [tableView contentOffset];
tableViewCenter.y += myTable.frame.size.height/2;
[tableView setContentOffset:CGPointMake(0,theCell.center.y-65) animated:YES];
[tableView reloadData];
}
使用 [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:YES];
可以滚动表视图使得指定的行在屏幕上的特定位置可见。
还有...
scrollToNearestSelectedRowAtScrollPosition:animated:
滚动表格视图以使离指定位置最近的选中行位于该位置。
setContentOffset
方法,可能会导致您的表视图/集合视图跳动一下。我建议您尝试另一种方法。一个建议是使用您免费获得的滚动视图委托方法。只需一行代码:
self.tblViewMessages.scrollToRow(at: IndexPath.init(row: arrayChat.count-1, section: 0), at: .bottom, animated: isAnimeted)
challengeInfoTableView.scrollRectToVisible(challengeInfoCell.challengeDescriptionTextView!.frame, animated: false)