简体   繁体   English

UICollection视图更新回调

[英]UICollection view update callback

I have been googling about this but is there an easy way to catch the event when data in collection has been added or removed? 我一直在谷歌上搜索,但是当添加或删除集合中的数据时,是否有一种简单的方法来捕获事件? I have collection view containing list of images in one line horizontally and user can scroll right or left to see the images. 我有一个包含视图的集合视图,该视图在水平方向上排成一行,用户可以向右或向左滚动以查看图像。 I want to bring the scroll position to the first image from the right when another image is added. 我想在添加其他图像时将滚动位置从右侧移到第一幅图像。 I can do that by using scrollRectToVisible but I am not sure where I can call this function to scroll. 我可以通过使用scrollRectToVisible来做到这一点,但不确定在哪里可以调用此函数进行滚动。 I was trying to find an event fired when the collection view is modified. 我试图查找修改集合视图时触发的事件。

And how exactly are you adding the image? 以及您如何精确添加图像? Are you adding a new cell? 您要添加一个新的单元格吗? If yes then you must be updating the layout parameters and invalidating the layout, you can add your scroll there. 如果是,则必须更新布局参数并使布局无效,您可以在此处添加滚动条。 And if you are just adding an image on the already existing cell, then on what gesture are you doing it? 并且,如果您只是在现有单元格上添加图像,那么您将以什么姿态进行操作? Gesture Handler is a good place in that case. 在这种情况下,手势处理程序是个好地方。

This is bad architecture. 这是糟糕的架构。 Instead of the relevant parts of your controller communicating with one another, you're trying to bounce the information off your view layer. 您正在尝试将信息从视图层退回,而不是让控制器的相关部分相互通信。 Don't do this. 不要这样 This is not what the view layer is for. 这不是视图层的用途。 When you have new information and update the collection view, update its scroll position as well. 当您有新信息并更新集合视图时,也要更新其滚动位置。

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

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