iProRage
iProRage

Reputation: 424

'reset' table view cell's content

my app has a bug, and i believe its because I'm not reseting the cells content. The apple docs say

The table view’s data source implementation of tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell.

Could someone please explain how to do this, or point me to a tutorial? Thank you in advance!

My cellForRow

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
     {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

         addBtn = [[UIButton alloc]init];
         addBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
         [addBtn setFrame:CGRectMake(220,10,25,55)];
         [addBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
         [addBtn setTitle:@"+" forState:UIControlStateNormal];
         [addBtn setEnabled:YES];
         [cell addSubview:addBtn];

         subBtn = [[UIButton alloc]init];
         subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
         [subBtn setFrame:CGRectMake(260,10,25,55)];
         [subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
         [subBtn setTitle:@"-" forState:UIControlStateNormal];
         [subBtn setEnabled:YES];
         [cell addSubview:subBtn];

     } 
    //cellText.hidden=!self.editing;
    cell.textLabel.textColor = [UIColor orangeColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];  
    cell.textLabel.text = [number objectAtIndex:indexPath.row];// <------ Is this line in the right place?
    cell.textLabel.text = @"1"; // <---- Is this line in the right place? 


return cell;
}

Upvotes: 4

Views: 5171

Answers (3)

Michael Frederick
Michael Frederick

Reputation: 16714

See the comments in the code below...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellID";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
        // this is where you want to configure your generic cell
        // for example, if EVERY cell needs to have a disclosure indicator, you could do
        cell.accessoryType = UITableViewAccessoryTypeDisclosurseIndicator;    
    }

    // this is where you want to put code that would or could be unique to a cell
    // for example, if you wanted to put the row number you could do:
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    // you would put this here and not in the above if statement because the value of the textLabel changes for different cells

    return cell;
}

Upvotes: 1

Ecarrion
Ecarrion

Reputation: 4950

If you're adding views programmatically to the cell, you should do the following for cleaning them:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellID";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];        
    }

    for (UIView * v in cell.contentView.subviews) {        
        [v removeFromSuperview]
    }

    //Configure your cell

    return cell;
}

Upvotes: 1

Owen Hartnett
Owen Hartnett

Reputation: 5935

It's pretty simple. You provide the call cellForRowAtIndexPath in your code. In it, you either provide a brand new cell, or you reuse a cell that the OS has chucked into memory. Basically, you code looks like this:

    static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
[[cell textLabel] setText:[pickerData objectAtIndex:indexPath.row]];
 cell.accessoryType = (indexPath.row == currentValueOfIndex ) ? 
    UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;


return cell;

The part under the comment // Configure the cell is where you have to reset your cells content. Since it can either be a new cell, or a recycled cell, it will either have no information, or the information from another cell where it was recycled from. In both cases, you provide the cell's text and accessories and anything else you want to use each time the cell is called. Basically, it's a callback to you to provide a cell, either a brand new one, or a recycled one, and you have to stuff the right info for the cell it's going to be (based on the index path). Hope I made this clear enough.

Upvotes: 2

Related Questions