在xcode中发布动态添加的tableview子视图

时间:2011-09-19 12:49:00

标签: iphone xcode

我正在处理一个应用程序,其中我使用自定义单元格创建了一个表格视图。在自定义单元格中我添​​加了imageview.i正在解析服务器中的图像并将该图像添加到tableview现在当用户按下删除按钮我想要重新加载tableview数据,但问题是以前添加的子视图没有发布。我也使用代码发布它但它不起作用。 以下是tableview的示例代码和图像

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];
}

enter image description here

3 个答案:

答案 0 :(得分:1)

确保在将子视图添加到视图后将其释放,因为视图将保留它们。

答案 1 :(得分:1)

在“cellForRowAtIndexPath”表格方法中,在添加图片子视图之前,请在代码下面添加代码。

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

“cell”也应初始化一次。

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++;
}

返回单元格;

您已更新代码。请查看它。

干杯

答案 2 :(得分:0)

enter image description here

//格式化功能

    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;