ios UITableView 移动到单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10269587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:59:14  来源:igfitidea点击:

UITableView move to cell

iosuitableview

提问by Matrosov Alexander

I want to make next:

我想做下一个:

I have an UITableView that have to dispaly words (A-Z).

我有一个 UITableView 必须显示单词(AZ)。

Currently when view did load I have one cell that displayed (and this is correct). First cell display first word from my array words.

目前,当视图确实加载时,我显示了一个单元格(这是正确的)。第一个单元格显示数组单词中的第一个单词。

Purpose: I want to move to the cell that must display 10 word from my array, but problem is that the cell with indexPath.row = 10 does not exist (and this correct, because I don't scroll yet).

目的:我想移动到必须从数组中显示 10 个单词的单元格,但问题是 indexPath.row = 10 的单元格不存在(这是正确的,因为我还没有滚动)。

What is a right wait to make transition from 1 to 10 cell.

从 1 单元格过渡到 10 单元格的正确等待是什么?

I think if I don't use dequeueReusableCellWithIdentifierfor creating my cell I can do it and solve my problem, but I mean this problem for device memory.

我想如果我不dequeueReusableCellWithIdentifier用于创建我的单元格我可以做到并解决我的问题,但我的意思是设备内存的这个问题。

In other words I need to make scrollToRowAtIndexPath

换句话说,我需要做 scrollToRowAtIndexPath

Thanks!

谢谢!

回答by arooaroo

You are right to identify the scrollToRowAtIndexPathmethod. So all you need to do is create a fresh IndexPath with the row you wish to move to, e.g., row index = 10:

你是正确的识别scrollToRowAtIndexPath方法。所以你需要做的就是用你想要移动到的行创建一个新的 IndexPath,例如,行索引 = 10:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:indexPath.section] 
             atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

回答by A R

//For iOS 7 
        [self.TableView reloadData];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
        [TableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

回答by A R

One can identify the row(in the eg. below it is forRow) that needs to be made visible and reload the row. Upon the completion of that one can scroll to that row.

可以识别需要使其可见的行(例如,下面是 forRow)并重新加载该行。完成后可以滚动到该行。

let moveToIndexPath = NSIndexPath(forRow: forRow, inSection: 0)
self.tableView.reloadRowsAtIndexPaths([moveToIndexPath], withRowAnimation: .None)
self.tableView.scrollToRowAtIndexPath(moveToIndexPath, atScrollPosition: atScrollPosition, animated: true)