问题描述
我有一个带有自定义单元格的TableView,其中一些包含图片,而另一些则没有。一切正常,但是当我开始滚动时,单元格显示不正确(图像放置在错误的单元格中,并在中间剪切)。现在有人解决该问题了吗?
I have a TableView with custom cells, some of which have images and others don't have. Everything works fine but when I start scrolling the cells aren't showing correctly (the images are placed in "wrong" cells and cut in the middle). Does anyone now how to fix this?
推荐答案
要理解的关键是表的单元格已被重用。这样做是为了提高滚动性能,因为创建一个新的单元格(即视图)非常昂贵。
The key thing to understand is that your table's cells are reused. This is done to increase the performance for scrolling because creating a new cell - which is a view - is quite expensive.
在方法cellForRowAtIndexPath中,您将返回一个单元格。使用出队方法创建现有单元,或者创建一个新单元。在该单元格上,设置您希望代表的所有属性。
In the method cellForRowAtIndexPath, you return a cell. Either an existing cell using the dequeue method, or you create a new one. On that cell, you set all the properties of whatever it is you wish to represent.
我的猜测是,您正在设置这些属性,例如图像等。单元格直接。然后,当您滚动时,该单元格将被重用,并且您会在错误的单元格上再次看到相同的图像。
My guess is that you are setting these properties such as image, etc. on the cell directly. Then when you scroll, that cell is reused and you see the same image again but on the wrong cell.
因此,您需要的是一个单独的索引,例如NSArray维护模型对象。该模型对象包含图像的所有详细信息,等等。
So, what you need is a separate index like an NSArray to maintain your model objects. That model object contains all the details of image, etc.
让我们举个例子。我有一个包含10个对象的数组,其中前五个是图像 a.png,后五个是 b.png。
Let's take an example. I have an array with 10 objects, the first five of which are image "a.png" and the second five "b.png".
如果收到回调关于cellForRowAtIndexPath,我想做如下操作:
If I receive a callback of cellForRowAtIndexPath, I want to do as follows:
Now, let's consider that in the UITableView, underneath the covers, five of the cells are being reused.
对于上述每次调用,都会发生以下情况:
For each call above, the following happens:
cellForRowAtIndexPath:(0,2)为数组中的对象提供索引2,即a.png
-您尝试使现有单元出队,但它返回 nil,因此您创建了一个新单元。您可以设置模型对象的详细信息。
cellForRowAtIndexPath:(0,2) provide object from array a index 2, which is a.png -- you attempt to dequeue an existing cell but it comes back "nil" so you create a new cell. You set the details of your model object.
cellForRowAtIndexPath:(0,3)为数组中的对象提供索引3,即a.png
-您尝试使现有单元出队,但它返回 nil,因此您创建了一个新单元。您可以设置模型对象的详细信息。
cellForRowAtIndexPath:(0,3) provide object from array a index 3, which is a.png -- you attempt to dequeue an existing cell but it comes back "nil" so you create a new cell. You set the details of your model object.
cellForRowAtIndexPath:(0,4)为数组中的对象提供索引4,即a.png
-您尝试使现有单元出队,但它返回 nil,因此您创建了一个新单元。您可以设置模型对象的详细信息。
cellForRowAtIndexPath:(0,4) provide object from array a index 4, which is a.png -- you attempt to dequeue an existing cell but it comes back "nil" so you create a new cell. You set the details of your model object.
cellForRowAtIndexPath:(0,5)为数组中的对象提供索引5,即b.png
-您尝试使现有单元出队,并且可以正常工作!您可以通过设置模型对象的详细信息再次使用该单元格
cellForRowAtIndexPath:(0,5) provide object from array a index 5, which is b.png -- you attempt to dequeue an existing cell and it works! you can use that cell again by setting the details of your model object
cellForRowAtIndexPath:(0,6)为数组中的对象提供索引6,即b.png
-您尝试使现有单元出队,并且可以使用!您可以通过设置模型对象的详细信息再次使用该单元格
cellForRowAtIndexPath:(0,6) provide object from array a index 6, which is b.png -- you attempt to dequeue an existing cell and it works! you can use that cell again by setting the details of your model object
cellForRowAtIndexPath:(0,7)为数组中的对象提供索引7,即b.png
-您尝试使现有单元出队,并且可以使用!您可以通过设置模型对象的详细信息再次使用该单元格
cellForRowAtIndexPath:(0,7) provide object from array a index 7, which is b.png -- you attempt to dequeue an existing cell and it works! you can use that cell again by setting the details of your model object
cellForRowAtIndexPath:(0,8)为数组中的对象提供索引8,即b.png
-您尝试使现有单元出队,并且可以使用!您可以通过设置模型对象的详细信息再次使用该单元格
cellForRowAtIndexPath:(0,8) provide object from array a index 8, which is b.png -- you attempt to dequeue an existing cell and it works! you can use that cell again by setting the details of your model object
cellForRowAtIndexPath:(0,9)为数组中的对象提供索引9,即b.png
-您尝试使现有单元出队,并且可以使用!您可以通过设置模型对象的详细信息再次使用该单元格。
cellForRowAtIndexPath:(0,9) provide object from array a index 9, which is b.png -- you attempt to dequeue an existing cell and it works! you can use that cell again by setting the details of your model object
抱歉,太冗长了!我希望这有帮助。也许值得学习一个好的表格视图教程。当然会有很多。
Sorry for being so verbose! I hope this helps. Maybe it's worth working through a good table view tutorial. There will be many out there for sure.
这篇关于UITableView滚动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!