TheCoder
TheCoder

Reputation: 33

Dequeued UICollectionViewCell not refreshed

I have an application with a UICollectionView with horizontal scroll of cells (Only one cell is displayed at a time). Each cell hosts a UITableView which displays content (URL links) in each of the UITableViewCell.
The problem is that the first two cells of UICollectionView 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 UITableView 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 UICollectionViewCell that hosts the UITableView. The custom cell implements the UITableViewDataSource, UITableViewDelegate.
I have searched through different forums and sites and found the solution of refresh. I tried adding the [collectionView reload] to the -cellForItemAtIndexPath: 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?

Upvotes: 3

Views: 1429

Answers (2)

Kesong Xie
Kesong Xie

Reputation: 1396

I had similar problem, instead of using [cell.tableView reloadData] inside the cellForItemAtIndexPath, I called collectionView.reloadData in the prepareForUse method that is overwritten in your custom UITableviewCell

Upvotes: 0

Petro Korienev
Petro Korienev

Reputation: 4027

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

Upvotes: 4

Related Questions