【问题标题】:How to scroll UITableView to specific position如何将 UITableView 滚动到特定位置
【发布时间】:2011-08-16 22:03:06
【问题描述】:

如何将表格的单元格滚动到特定位置?我有一个显示 3 行的表格(根据高度)。我想要的是,如果我点击第一行而不是根据表格的高度,第一行应该滚动并获得新的位置(中心),其他行也是如此。我尝试了 contenOffset 但没有用..

已编辑:

简而言之,当我们在选择器中选择任何行时,数据选择器之类的东西会滚动到中心。

谢谢..

【问题讨论】:

  • 如果我向下滚动到列表中的第十个项目并且我的显示器显示项目 10、11、12。你是说如果我单击第一行(即项目 10)使项目 10 居中在屏幕上有效地显示第 9、10、11 项?
  • ya...类似于数据选择器...

标签: iphone objective-c cocoa-touch uitableview uiscrollview


【解决方案1】:

它应该可以使用- (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;

希望对你有帮助

干杯

【讨论】:

  • 当我选择行时会调用这个方法吗?
  • 不,调用时会选择一行。当您选择一行时,它不会被调用。有细微的差别。
  • 我希望表格在选择行时滚动...如何根据当前表格的高度设置单元格的 contentOffset ?
  • 当您选择一行时,UITableViewDelegate 中的方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 会被调用,因此请在该方法中调用scrollToRowAtIndexPath。我强烈建议您阅读Table View Programming Guide for iOS 以及UITableViewreference 和UITableViewDelegate 参考,您会发现它们非常有用,毕竟这就是文档的用途
  • 这应该是选择的答案。
【解决方案2】:
[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

这会将您的表格视图带到第一行。

【讨论】:

  • @Parveen 谢谢它对我有很大帮助
  • xamarin 开发者的回答:tblRecord.ScrollRectToVisible(CoreGraphics.CGRect.FromLTRB(0, 0, 1, 1), true);
【解决方案3】:

Swift 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:

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()
    }

}

【讨论】:

    【解决方案4】:

    最后我发现...当表格只显示 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 setContentOffset:CGPointMake(0,theCell.center.y-65) animated:YES]; - -65 不好。考虑使用 65 的代码,例如 theCell.frame.size.height。
    • @Rocotilos 是的,我同意。但我已经明确提到“当表格只显示 3 行时它会很好用......如果行更多,应该相应地改变......”这只是一个示例代码。我也用这个表作为 UIPicker。
    • 它不起作用,除此之外还有比这更好的解决方案。
    • 我们可以使用一个计数器值来存储我们想要显示的行数,如果我们想显示更多行我们应该改变计数器值
    【解决方案5】:

    使用[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:YES]; 滚动接收器,直到由索引路径标识的行位于屏幕上的特定位置。

    scrollToNearestSelectedRowAtScrollPosition:animated:
    

    滚动表格视图,使选定的最接近表格视图中指定位置的行位于该位置。

    【讨论】:

    • 我如何计算桌子的高度?意味着我必须计算所有行或仅可见行的表格高度吗?
    • 或者说,如何根据当前表格的高度设置单元格的 contentOffset ?我需要用所有行还是只用可见行来计算表格的高度?
    • 如果它的标准表格单元格的高度为 44。
    【解决方案6】:

    值得注意的是,如果你使用setContentOffset的方式,可能会导致你的table view/collection view跳转一点。老实说,我会尝试另一种方式来解决这个问题。建议使用免费提供的滚动视图委托方法。

    【讨论】:

      【解决方案7】:

      只需一行代码:

      self.tblViewMessages.scrollToRow(at: IndexPath.init(row: arrayChat.count-1, section: 0), at: .bottom, animated: isAnimeted)
      

      【讨论】:

        【解决方案8】:

        如果您的表格视图只有一行和一个部分,您也可以使用以下方法

         challengeInfoTableView.scrollRectToVisible(challengeInfoCell.challengeDescriptionTextView!.frame, animated: false)
        

        在这种情况下,您可以直接滚动到正确的文本字段、文本视图、标签等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多