TheCoder TheCoder - 1 year ago 135
iOS Question

Dequeued UICollectionViewCell not refreshed

I have an application with a

with horizontal scroll of cells (Only one cell is displayed at a time). Each cell hosts a
which displays content (URL links) in each of the

The problem is that the first two cells of
display the proper content, but starting from the third cell, the contents are displayed from the dequeued cells. The correct content on the dequeued cell's
is only displayed if user scrolls down and then scrolls up to the top.

For example, the contents of the third cell are the content from the first cell.
The cell is a custom
that hosts the
. The custom cell implements the

I have searched through different forums and sites and found the solution of refresh.
I tried adding the
[collectionView reload]
to the
method, but it goes into constant loop. I know that is not the right place, but i do not have any other methods that cause the refresh (only scrolling through the cells loads the next cell's content).

Can somebody please help me to resolve the issue?

Answer Source

You are missing some facts about dequeuing.
When you 3d custom UICollectionViewCell is dequeued, it isn't allocated like when it's displayed for the first time. Instead, your collection view "reuses" 1st cell for displaying 3d. The best moment to clear content in your custom cell class is -(void)prepareForReuse. If you override it and clear it's content, the -cellForItemAtIndexPath: will dequeue "clear" cell which you can further customize.
However, i don't exactly know how to "clear" the tableView, not sure whether it's possible. You have to reloadData on your tableView in -cellForItemAtIndexPath:. Example:

- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
    MyCVCell *cell = [collectionView dequeueReusableCellWithIdentifier:@"MyCVCell" forIndexPath:indexPath];
    cell.arrayOfURLsForDisplay = self.arrayOfArraysOfURLs[indexPath.row];
    [cell.tableView reloadData];
    return cell;

Here i mean you have some arrayOfArraysOfURLs in your viewController and you dislay URLs in your custom cell from some arrayOfURLsForDisplay

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download