Collectionview Duplicate Cell When Loading More Data

CollectionView duplicate cell when loading more data

This is a typical table view/collection view setup issue.

Any time you recycle a cell using a dequeue method like dequeueReusableCellWithReuseIdentifier:, you must always fully configure all the views in the cell, including setting all the text fields/image views to their starting values. Your code has several if statements where, if the condition of the if is false, you don't set up the views in the cell. You need to have else clauses that clear out old content from the cell's views in case there is content left over from the last time the cell was used.

EDIT:

Change your cellForItemAtIndexPath method to begin like this:

func collectionView(collectionView: UICollectionView, 
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell =
collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,
forIndexPath: indexPath) as! UpcomingCollectionViewCell
cell.imageView.image = nil; //Remove the image from the recycled cell
//The rest of your method ...

CollectionView cell displaying duplicated data when scrolling?

This is a common issue everybody encounters with collection views and table views when they first start using them. The issue is that cells get recycled. When you call dequeueReusableCell after scrolling, you are likely to get passed a cell that just scrolled off-screen and has contents in its view from the last use. You have to make your cellForItemAt/cellForRowAt code always set a value for every field, regardless of the model data.

Your cellForItemAt() code uses optional chaining to only set cell views if various parts of your model are not nil (the question marks in postCategory?.post?[indexPath.row] and the line below). You need to instead use if let or some other approach that always sets each field to a value, and sets it to an empty value if the corresponding part of your data model is nil.

Also note that you should not ever call reloadData() inside cellForItemAt. That will cause an endless loop where the collection view tries to return a cell, then throws all the cells away and starts loading new ones, but each time it returns a new cell, throws them all away again.

Collection view cells duplicate when scrolling

The problem is that you don't specify what text to appear on each of your CollectionViewCell's textView. As long as you don't specify the same in cellForItemAt indexPath it is going to show the reused cell and its content, from dequeueReusableCell as you said.

For the solution to your specific problem you can do as below in the viewcontroller:

`var textViewContentArray = [Int: String]()` //Create a dictionary to hold the texts globally

In textViewDidEndEditing:

func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.textColor = .gray
textView.text = "Chapter Title"
} else {
guard let cell = textView.superview?.superview as? CustomWriterPageCell else {
return
}
guard let indexPath = self.collectionView.indexPath(for: cell) else { return }
textViewContentArray[indexPath.row] = textView.text
}
}

In textViewDidBeginEditing:

func textViewDidBeginEditing(_ textView: UITextView) {
textView.textColor = .black
}

And in cellForItemAt indexPath:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomWriterPageCell", for: indexPath) as! CustomWriterPageCell
cell.textView.delegate = self
let text = textViewContentArray[indexPath.row]
if !text.isEmpty {
cell.textView.textColor = .black
cell.textView.text = text
} else {
cell.textView.textColor = .gray
cell.textView.text = "Chapter Title"
}
return cell
}

Note: Here I am assuming you have set the Controller which holds the collectionView as delegate for the textViewInCell, if the cell is the delegate you can update the textViewContentArray using protocol

Hope this adds to your understanding.

UICollectionViewCell duplication

Since you use

ref?.observe(.value, with:
{ (snapshot) in

you'll be in threat that this callback is called every edit that occurs to it , which leads to an unexpected results as you encounter like duplicating data inside the array , so you need a single observe

ref?.observeSingleEvent(.value, with:
{ (snapshot) in

Duplicate data is loading when I scroll the UICollectionView

-> Put this code above the line where your UIImageView’s object is created

for (id subview in cell.contentView.subviews) {
if ([subview isKindOfClass:[UIImageView class]]) {
[subview removeFromSuperview];
} else if ([subview isKindOfClass:[UILabel class]]) {
[subview removeFromSuperview];
}
}
  • It will remove subviews of content view of type UILabel and UIImageView.

Using tag:

adding a tag to a view as per you code
for UIImageView

self.image.tag = 101;

for UILabel

 self.NameLabel.tag = 102;

remove view using tag

//remove image view
UIView *view = [cell.contentView viewWithTag:101];
[view removeFromSuperview];

//remove label
view = [cell.contentView viewWithTag:101];
[view removeFromSuperview];

Swift - UICollectionView repeats (duplicate) cells

I think you do not understand how the UITableView and UICollectionView delegate methods work.

iOS will not load 100 cells at the same time because it will be very slow and not a good user experience. So, the methods cellForItemAtIndexPath or cellForRowAtIndexPath are called only when the cells appear on the screen.

Whenever you scroll down, you will see the method cellForItemAtIndexPathis called again to make the new cells. If you put the code println(indexPath.row) inside cellForItemAtIndexPath, you will actually see the cell number is printed repeatedly when you scroll up or down.

If you see the same cells after you scroll down, it might be due to your code on cellForItemAtIndexPath. I am unable to help you unless you paste the full source code above. Or it might be a bug in UICollectionView for XCode GM seed.

How about trying the same project in XCode 6 Beta 7 and report back? I am having a problem with UICollectionView in XCode GM as well. IT is related to constraint. See: iOS 8 GM does not update constraints on collection views I am now using XCode 6 Beta 7 due to that.

Duplicate collectionView cells after reloadData with Firebase query

messages array must be reset before repopulate snapshot document. You can add self.messages.removeAll() before the line for document in snapshot.documents



Related Topics



Leave a reply



Submit