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?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- how do you prevent page scroll in textarea on mobi
- Custom UITableview cell accessibility not working
相关文章
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- Unable to process app at this time due to a genera
- Popover segue to static cell UITableView causes co
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- TransitionFromView removes previous view
- Open iOS 11 Files app via URL Scheme or some other
You probably add images when you create the cell instances, instead of when the table view asks for a cell view. Example:
This is the right way. We set the image every time the table view asks for a cell.
This is the wrong way. We set the image when we create the cell. This will cause cells to have wrong images when they are re-used.
Try with changing the identifier value for your cell .
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.
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.
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.
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".
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:
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.