jogshardik
jogshardik

Reputation: 1456

UItableviewcell Reuse issue in ios

Hello guys i think almost everyone who is in ios development may come across the issue of reuse of the UITableCell by using following code line.

 RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

I have search lot about this but not getting any desire answer so please help me out in this case.

I have the same issue as most of iPhone developer having after reusing the cell. I have the UIProgressView inside my cell and one button is there for downloading the video and i am showing the progress there in progress view how much is left. So Now what i have problem is when i have more data and going out of the screen at that time i am press the download button on very first row of the UITableviewCell then i am scrolling down so the progress also shown in bottom random one cell so the UI changes in two cell rather then one.

Upvotes: 1

Views: 1507

Answers (3)

KavyaKavita
KavyaKavita

Reputation: 1579

Use prepareforreuse method to clear content of cell before using it... e.g.

-(void)prepareForReuse
{
    [super prepareForReuse];

    self.textLabel.text = @"";
    self.detailTextLabel.text = @"";
    self.imageView.image = nil;
} 

Upvotes: 0

AechoLiu
AechoLiu

Reputation: 18368

You need to assign a progress value inside the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // If the cell is reused, the `-prepareForReuse:` of `UITableViewCell` will be called.

    //!! Assign current progress value to this cell, otherwise, the progressBar.value may look like a random value. 
    //!! Because the current cell is reused from a disappeared cell.
    cell.progressBar.value = ... ;

    return cell;
}

The design may be complex, because the progress may be updated continuously when the cell is on the screen.

Upvotes: 0

Vijay
Vijay

Reputation: 801

You need to implement -prepareForReuse method in your custom cell class and set all cell properties to default value.

- (void)prepareForReuse

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

Refer here for more, https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse

Upvotes: 1

Related Questions