简体   繁体   English

UICollectionView滚动到特定单元格

[英]UICollectionView scroll to specific cell

i try to create a UICollectionView who should open at a specific cell. 我尝试创建一个应该在特定单元格打开的UICollectionView

My approach is to store all cells: 我的方法是存储所有单元格:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
        UICollectionViewCell {
  // my code
  cells.append(cell)
  return cell
}

then find the desired cell in viewDidAppear : 然后在viewDidAppear找到所需的单元格:

let result:[MyCell] = cells.filter{ $0.myProperty!.uuid == selectedUUID }

Unfortunately the desired cell can't be found within the cells array, because not all cells are stored yet. 不幸的是,在单元格数组中找不到所需的单元格,因为尚未存储所有单元格。

Am i going in the right direction or do i need a different solution? 我是朝着正确的方向前进还是需要其他解决方案?

EDIT: removed misleading information 编辑:删除了误导性信息

Don't work with the cell themselves. 不要自己处理单元。

Your cells are displaying data, right? 您的单元格正在显示数据,对吧? They must. 他们一定。 That data (could be numbers, or text, or an image, anything) should be a custom object. 该数据(可以是数字,文本,图像或其他任何东西)应该是一个自定义对象。

For example, you're displaying a list of animals. 例如,您正在显示动物列表。

So you have the Animal class, with different properties ( Size , weight , age ). 因此,您获得了Animal类,具有不同的属性( Sizeweightage )。 Again, all an example. 同样,所有示例。 Maybe you're displaying a list of students which have a FirstName, Lastname, and so on. 也许您正在显示具有名字,姓氏等的学生列表。 Lets say we're using the Animal . 可以说我们正在使用Animal

You have an array of Animal objects, you can have 1, or 4, or 234234, it doesn't matter. 您有一个Animal对象数组,可以有1个,4个或234234个,没关系。 The array can hold as many animals as you want. 该阵列可以容纳任意数量的动物。 But you only have one array. 但是您只有一个数组。

Your collection view (or tableview) needs to create a cell for each row. 您的集合视图(或表视图)需要为每一行创建一个单元格。 How many rows?! 多少行? Easy, the other methods tells us there are a specific number of rows per section. 很简单,其他方法告诉我们每个节有特定数量的行。 Let's say we only have one section. 假设我们只有一个部分。

How do you define rows? 您如何定义行? You give him a number. 你给他一个电话。 What number? 什么号码? The number of animals in your array. 您的阵列中的动物数量。 For example, 5. Or, much better, myAnimalsArray.Count . 例如5。或者更好的是myAnimalsArray.Count Now if you have 20 animals, you'll have 20 cells. 现在,如果您有20只动物,那么您将拥有20个细胞。 If you have 3 animals, you'll have 3 cells. 如果您有3只动物,那么您将拥有3个单元格。

Okay, now to the cells, you know how many you have, and the cellForRow will therefore create X cells, X being the count of your array. 好的,现在到单元格了,您知道您有多少个单元格,因此cellForRow将创建X个单元格,X是数组的数量。

You'll probably have a simple cell for testing, like 您可能会有一个简单的测试单元,例如

cell.title = animal.name

and then return the cell. 然后返回单元格。 It's all pseudo code here, but you get the idea. 这里都是伪代码,但是您知道了。

Now to your actual question 现在到您的实际问题

I've written all that because, according to your other comments in the other answers, you seem pretty confused. 我写这些都是因为,根据您在其他答案中的其他评论,您似乎很困惑。

You have your animal array (the data), and no cells yet. 您拥有动物数组(数据),还没有细胞。 You want to start on the animal that has a name starting with E, for example. 例如,您要以名称以E开头的动物开始。 Well, after you've all the animals on the array, you need to find the one that starts with E (or any other filter you might like). 好了,将所有动物放入数组后,您需要找到以E开头的动物(或您可能喜欢的任何其他过滤器)。 You find it with a searching method. 您可以使用搜索方法找到它。 Once its done, you will know the index of the animal in the array. 一旦完成,您将知道动物在数组中的索引。 It's the array item number 23, it's the Elephant. 这是数组项编号23,这是大象。

Create an NSIndexPath object with section 0 and index 23 (because your filter found it's the 23rd item, again, i'm just using examples), and pass that indexPath object to the scroll method of the collectionview . 创建一个带有第0部分和索引23的NSIndexPath对象(因为您的过滤器再次发现它是第23个项目,我只是在使用示例),然后将该indexPath对象传递给collectionviewscroll方法

And you're good ;) 而且你很好;)

Do that in the ViewDidLayoutSubviews or viewDidAppear . ViewDidLayoutSubviewsviewDidAppear (first one is better). (第一个更好)。 But you must do it after your array of animals is loaded, otherwise you won't have any data to filter. 但是您必须加载动物数组之后执行此操作,否则将没有任何数据可筛选。

Once you've called the method in the link above, your collectionview will scroll, and if you've done it soon enough, it will load at that index and not at index zero. 一旦您在上面的链接中调用了该方法,您的collectionview将滚动,并且如果您足够快地完成该操作,它将在该索引而不是零索引处加载。

i have solved this problem through this code hope it will helpful for you: 我已经通过这段代码解决了这个问题,希望对您有帮助:

[self.collectionview setContentOffset:CGPointMake(0,self.collectionview.contentSize.height-self.collectionview.frame.size.height/2) animated:YES];

set above only parameter to contentsize:CGSizemake(x,y); 将以上参数设置为contentsize:CGSizemake(x,y);

by adjusting your required parameter you can scroll to specific cell. 通过调整所需的参数,您可以滚动到特定的单元格。 just replace above parameter inplace of c and y in contentsize:CGSizemake(x,y); 只需将以上参数替换为contentsize:CGSizemake(x,y);的c和y contentsize:CGSizemake(x,y); method. 方法。

and you have to adjust parameter. 并且必须调整参数。

For your case, you need to filter data array instead of filtering cells array, this way you can get exact index. 对于您的情况,您需要过滤数据数组而不是过滤单元格数组,这样您可以获取准确的索引。

Based on the filteredIndex create indexpath using below method, 基于所述filteredIndex创建indexpath使用以下方法,

NSIndexPath(forItem: filteredIndex, inSection: yourSectionIndex)

This way you can scroll to particular cell. 这样,您可以滚动到特定的单元格。

Try adding a call to collectionView.reloadData() before scrollToItemAtIndexPath. 尝试在scrollToItemAtIndexPath之前添加对collectionView.reloadData()的调用。 This will ensure all required cells are created. 这将确保创建所有必需的单元。

UICollectionView's cell is reused . UICollectionView的单元格已重用
For example, if view can only show 3 cells once: A, B , C; 例如,如果视图只能显示3个单元格一次:A,B,C; then ur cells maybe like [A, B, C, A, B, C, A, B, ...] ; 那么我们的细胞可能像[A,B,C,A,B,C,A,B,...]

each time, u set the property of cell, uuid will be reset to a new value. 每次设置单元格的属性时,uuid都会重置为新值。 so u cant filter out what u want. 所以你无法过滤出你想要的东西。 Hitendra Hckr is right, u should "filter data array instead of filtering cells array". Hitendra Hckr是正确的,您应该“过滤数据数组而不是过滤单元格数组”。

Or u can " alloc " new cell instead of " dequeueReusableCellWithReuseIdentifier:forIndexPath: ". 或者,您可以“ 分配 ”新单元格,而不是“ dequeueReusableCellWithReuseIdentifier:forIndexPath: ”。 (Not recommend) (不推荐)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM