如何滚动UITableView到特定位置

编程入门 行业动态 更新时间:2024-10-26 14:35:49
本文介绍了如何滚动UITableView到特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将表格的储存格移至特定位置?我有一个表,显示3行(根据高度)。我想要的是,如果我点击第一行比根据表的高度第一行应滚动和获取新的位置(中心)和其他行相同。我尝试了contenOffset但没有工作..

已编辑:

div>

finally我发现...它会工作很好,当表显示只有3行...如果行更多的更改应该相应...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //自定义表视图中的行数。 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 30; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @细胞; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } //配置单元格。 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]; }

How can I scroll the table's cell to specific position ? I have a table which shows 3 rows (according to height). what I want is if I click on 1st row than according to table's height the 1st row should scroll and get new position (center) and same for other rows. I tried contenOffset but did not work..

EDITED :

In short some thing like data picker when we select any row in picker the row scrolls to the center.

Thanks..

解决方案

finally I found... it will work nice when table displays only 3 rows... if rows are more change should be accordingly...

- (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]; }

更多推荐

如何滚动UITableView到特定位置

本文发布于:2023-10-08 03:06:00,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!