0

I am using AQGridView for a grid of images.I need to overlay a progress bar over a particular image which is being downloaded. The problem is, if I scroll that Image cell out of view, the progress bar appears on another cell as well. I think this is because cell is being reused.

Is there a way i can mark certain cells from being reused?

mackworth
  • 5,873
  • 2
  • 29
  • 49
Shreesh
  • 677
  • 5
  • 15

3 Answers3

2

Please don't do that. You should update your cell in - gridView:cellForItemAtIndex:, which gets called for every cell that becomes visible.

Something like:

- (AQGridViewCell *)gridView:(AQGridView *)aGridView cellForItemAtIndex:(NSUInteger)index
{
     AQGridViewCell *cell;

     // dequeue cell or create if nil
     // ...

     MyItem *item = [items objectAtIndex:index];
     cell.progressView.hidden = !item.downloading;

     return cell;
}
ySgPjx
  • 10,165
  • 7
  • 61
  • 78
0

UITableViewCells will be reused by the tableview by default in order to reduce memory usage and increase efficiency, hence you shouldn't try to disable the reuse behavior (although it is possible). Instead of disabling the cell from being reused, you should check explicitly if the cell contains a loading image and show / hide the progress bar (and progress) as necessary, possibly through a flag.

If you still need to disable the reuse behavior, do not call dequeueTableCellWithIdentifier, but create new instances of the tableviewcells and explicitly keep references to it in cellForRowAtIndexPath. However this does not scale well and will end up consuming a lot more memory, especially if your tableview has many entries.

futureelite7
  • 11,462
  • 10
  • 53
  • 87
-1

Here's how I did it. In my derived cell class i have an instance variable

BOOL dontReuse;

I created a Category for AQGridView and defined dequeueReusableCellWithIdentifier as below:

    - (AQGridViewCell *) dequeueReusableCellWithIdentifier: (NSString *) reuseIdentifier AtIndex:(NSUInteger) index
{
    /* Be selfish and give back the same cell only for the specified index*/
    NSPredicate* predTrue = [NSPredicate predicateWithFormat:@"dontReuse == YES"];
    NSMutableSet * cells = [[[_reusableGridCells objectForKey: reuseIdentifier] filteredSetUsingPredicate:predTrue] mutableCopy];
    for(AQGridViewCell* cell in cells){
        if(index == [cell displayIndex]) {
            [[_reusableGridCells objectForKey: reuseIdentifier] removeObject: cell];
            return cell;
        }
    }

    NSPredicate* predFalse = [NSPredicate predicateWithFormat:@"dontReuse == NO"];
    cells = [[[_reusableGridCells objectForKey: reuseIdentifier] filteredSetUsingPredicate:predFalse] mutableCopy];

    AQGridViewCell * cell = [[cells anyObject] retain];
    if ( cell == nil )
        return ( nil );

    [cell prepareForReuse];

    [[_reusableGridCells objectForKey: reuseIdentifier] removeObject: cell];
    return ( [cell autorelease] );
}
Shreesh
  • 677
  • 5
  • 15