开发者

release dynamically added subviews of tableview in xcode

i am working on one application in which i have created one table view using custom cell.In custom cell i have added imageview.i am parsing images from server and add that images to tableview now when user press on delete button i want to reload tableview data but the problem is that previously added subviews are not released.I also use code for release it but it not works. Here is sample code and image of t开发者_如何转开发ableview

if (buttonIndex == 0)
{       
    [images removeObjectAtIndex:selectedImage];

    for(id object in [self.view subviews])
    {
        [object removeFromSuperview];
    }
    //for (UIImageView *aLabel in [tv subviews]) [aLabel removeFromSuperview];

    [self.myTableView reloadData];
}

release dynamically added subviews of tableview in xcode


Inside your "cellForRowAtIndexPath" table method, put below code before you add the image subview.

UIImageView *img = nil;
NSArray *Array = [cell subviews];
for (img in Array){
    if ([img isKindOfClass:[UIImageView class]]){
        [img removeFromSuperview];
    }
}

Also "cell" should be initialize once.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

CGFloat xcord_img,ycord_img,height_img,width_img;    
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

UIImageView *img = nil;
NSArray *Array = [cell subviews];
for (img in Array)
{
    if ([img isKindOfClass:[UIImageView class]]){
        [img removeFromSuperview];          
     }
}
UIButton *btn = nil;
NSArray *Array1 = [cell subviews];
for (btn in Array1){
    if ([btn isKindOfClass:[UIButton class]]){
        [btn removeFromSuperview];
    }
}


[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
space_img = 100;
xcord_img = 20;
ycord_img = 20;
height_img = 70;
width_img = 70;
int i=0; 


for(int j=0; j<rownum && indexPath.row*rownum+j<18; 
{
    NSString *tagValue = [NSString stringWithFormat:@"%d", indexPath.row*rownum+j];

magazineData *obj=[[magazineData alloc]init];
obj=[mData objectAtIndex:indexPath.row];

UIImage *imgs=[UIImage imageWithData:obj.mImage]; 
UIImageView *ImagesVw = [[UIImageView alloc] initWithImage:imgs];
ImagesVw.frame=CGRectMake(xcord_img+space_img*j, ycord_img, width_img, height_img);
UIButton *button = [[UIButton alloc] initWithFrame:ImagesVw.frame];
button.tag = [tagValue intValue];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor clearColor];
//[cell.contentView addSubview:button];
//[button release];
[cell addSubview:button];
//[cell.contentView addSubview:ImagesVw];      
[cell addSubview: ImagesVw];
[cell bringSubviewToFront: ImagesVw];

//[ImagesVw release];
i++;
}

return cell;

Hi have updated code. Please review it.

Cheers


Make sure you release the subviews after adding them to the view since the view will retain them.


release dynamically added subviews of tableview in xcode

//Format function

    CGFloat xcord_img,ycord_img,height_img,width_img;
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    space_img = 100;
    xcord_img = 20;
    ycord_img = 20;
    height_img = 70;
    width_img = 70;
    int i=0; 
    for(int j=0; j<rownum && indexPath.row*rownum+j<18; j++)
    {
        NSString *tagValue = [NSString stringWithFormat:@"%d", indexPath.row*rownum+j];

        magazineData *obj=[[magazineData alloc]init];
        obj=[mData objectAtIndex:indexPath.row];

        UIImage *imgs=[UIImage imageWithData:obj.mImage]; 
        UIImageView *ImagesVw = [[UIImageView alloc] initWithImage:imgs];
        ImagesVw.frame=CGRectMake(xcord_img+space_img*j, ycord_img, width_img, height_img);
        UIButton *button = [[UIButton alloc] initWithFrame:ImagesVw.frame];
        button.tag = [tagValue intValue];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor= [UIColor clearColor];
        [cell.contentView addSubview:button];
        [button release];
        [cell.contentView addSubview:ImagesVw];

        [ImagesVw release];
        i++;
    }
    return cell;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜