views:

21

answers:

1

Hi guys,

I have a little table that works pretty well. But I was told to remove my subviews at some stage incase the call to drawn the table happens again for whatever reason. I was told it can be handled using removeFromSuperview.

Where and how should i handle this? I need to use tags to identify the cells or views?

Here is my code to create my views.

- (UITableViewCell *)tableView:(UITableView *)tableView  
cellForRowAtIndexPath:(NSIndexPath     *)indexPath 
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];
}

[cell setFont:[UIFont systemFontOfSize:10.0]];

if (indexPath.row == 0) 
{
    [cell setText:@"Show only Airports"];
    showAirportsOnlySegment = [ [ UISegmentedControl alloc ] initWithFrame: 
CGRectMake(100, 4, 80, 28) ];
    [ showAirportsOnlySegment insertSegmentWithTitle: @"Yes" atIndex: 0 
animated: NO ];
    [ showAirportsOnlySegment insertSegmentWithTitle: @"No" atIndex: 1 animated: 
NO ];
    showAirportsOnlySegment.selectedSegmentIndex = 0;
    showAirportsOnlySegment.segmentedControlStyle = UISegmentedControlStyleBar;
    showAirportsOnlySegment.tag = 1;
    showAirportsOnlySegment.enabled = YES;
    [showAirportsOnlySegment addTarget:self action:@selector(airConOnOffAction:) 
forControlEvents:UIControlEventValueChanged];
    [ cell addSubview: showAirportsOnlySegment ];
    [self.view bringSubviewToFront:showAirportsOnlySegment];        
    [showAirportsOnlySegment release];
} 
else if (indexPath.row == 1) 
{
    [cell setText:@"Air Con?"];     
    airConSegment = [ [ UISegmentedControl alloc ] initWithFrame: 
CGRectMake(100, 4, 80, 28) ];
    [ airConSegment insertSegmentWithTitle: @"Yes" atIndex: 0 animated: NO ];
    [ airConSegment insertSegmentWithTitle: @"No" atIndex: 1 animated: NO ];
    airConSegment.selectedSegmentIndex = 0;
    airConSegment.segmentedControlStyle = UISegmentedControlStyleBar;
    airConSegment.tag = 2;
    [airConSegment addTarget:self action:@selector(airConOnOffAction:) 
forControlEvents:UIControlEventValueChanged];
    airConSegment.enabled = YES;
    [ cell addSubview: airConSegment ];
    [self.view bringSubviewToFront:airConSegment];
    [airConSegment release];
}   
return cell;
}

Can anybody advise how to handle the remove the subviews?

Many THanks -Code

A: 

I guess that you want to customize your first and second row. However, the cell might got reused because you are using dequeueReusableCellWithIdentifier, so your custom subviews might reappear again in other row if you don't clean up your cell properly after dequeuing. In this case I would suggest that you keep your subviews in instance variables, says view1 and view2, and after dequeuing you just do:

if (view1.superview == cell) {
    [view1 removeFromSuperview];
    [view1 release];
}
else if (view2.superview == cell) {
    [view2 removeFromSuperview];
    [view2 release];
}

then your subview will not appear in the reused cell.

tia
I insert those lines of code into my method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath after the code that gets the cell? And that will check if it needs to release the view? Thanks -Code
Code
I have updated my answer to include memory management.
tia
I was pretty sure that we do not need to release the views since as you can see they are released in the code above [airConSegment release]; or have i misunderstood this?
Code
I guess you are right. You also have to fix few points in your code so just ignore my retain/release if it does not sound right. The point here is how to remove it from the reused cell. However, I myself would keep everything retained and release it explicitly when I need to so I can sure it would not be dealloc accidentally e.g. removeFromSuperview but still need to use it later.
tia