在滚动的uitableview单元格中崩溃

时间:2012-08-24 14:01:34

标签: objective-c ios uitableview

当我滚动uitableview然后尝试在uitableview'输入数组中添加新项目时,我面临崩溃

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

    if([selected_table_string isEqualToString:@"table_for_category"])
    {
       [cell.contentView addSubview:[self tab_label:[NSString stringWithFormat:@"%@",[AppDelegate.category_arr objectAtIndex:indexPath.row]] fram:CGRectMake(10, 3, 200, 25)]];

        if([AppDelegate.selected_category_arr_item containsObject:[AppDelegate.category_arr objectAtIndex:indexPath.row ]])
        {
           [cell.contentView addSubview:[self img_name:@"chk_box_chk.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
        else
        {
           [cell.contentView addSubview:[self img_name:@"chk_box.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
    }

    else if ([selected_table_string isEqualToString:@"table_for_shape"])
    {
        [cell.contentView addSubview:[self tab_label:[NSString stringWithFormat:@"%@",[AppDelegate.shapeNames_arr objectAtIndex:indexPath.row]] fram:CGRectMake(10, 3, 200, 25)]];

        if([AppDelegate.selected_shapeNames_arrr_item containsObject:[AppDelegate.shapeNames_arr objectAtIndex:indexPath.row ]])
        {
            [cell.contentView addSubview:[self img_name:@"chk_box_chk.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
        else
        {
            [cell.contentView addSubview:[self img_name:@"chk_box.png" frm:CGRectMake(140, 3, 30, 25)]];
        }


    }
    else if ([selected_table_string isEqualToString:@"table_for_color"])
    {
        [cell.contentView addSubview:[self tab_label:[NSString stringWithFormat:@"%@",[AppDelegate.color_arr objectAtIndex:indexPath.row]] fram:CGRectMake(10, 3, 200, 25)]];

        if([AppDelegate.selected_color_arr_item containsObject:[AppDelegate.color_arr objectAtIndex:indexPath.row ]])
        {
            [cell.contentView addSubview:[self img_name:@"chk_box_chk.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
        else
        {
            [cell.contentView addSubview:[self img_name:@"chk_box.png" frm:CGRectMake(140, 3, 30, 25)]];
        }

    }
    else if ([selected_table_string isEqualToString:@"table_for_purity"])
    {
       [cell.contentView addSubview:[self tab_label:[NSString stringWithFormat:@"%@",[AppDelegate.purity_arr objectAtIndex:indexPath.row]] fram:CGRectMake(10, 3, 200, 25)]];

        if([AppDelegate.selected_purity_arr_item containsObject:[AppDelegate.purity_arr objectAtIndex:indexPath.row ]])
        {
           [cell.contentView addSubview:[self img_name:@"chk_box_chk.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
        else
        {
            [cell.contentView addSubview:[self img_name:@"chk_box.png" frm:CGRectMake(140, 3, 30, 25)]];
        }

    }
    else if ([selected_table_string isEqualToString:@"table_for_Fluo"])
    {
        [cell.contentView addSubview:[self tab_label:[NSString stringWithFormat:@"%@",[AppDelegate.Fluo_arr objectAtIndex:indexPath.row]] fram:CGRectMake(10, 3, 200, 25)]];

        if([AppDelegate.selected_Fluo_arr_item containsObject:[AppDelegate.Fluo_arr objectAtIndex:indexPath.row ]])
        {
            [cell.contentView addSubview:[self img_name:@"chk_box_chk.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
        else
        {
            [cell.contentView addSubview:[self img_name:@"chk_box.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
    }
    else if ([selected_table_string isEqualToString:@"table_for_Cut"])
    {
        [cell.contentView addSubview:[self tab_label:[NSString stringWithFormat:@"%@",[AppDelegate.cut_arr objectAtIndex:indexPath.row]] fram:CGRectMake(10, 3, 200, 25)]];

        if([AppDelegate.selected_cut_arr_item containsObject:[AppDelegate.cut_arr objectAtIndex:indexPath.row ]])
        {
            [cell.contentView addSubview:[self img_name:@"chk_box_chk.png" frm:CGRectMake(140, 3, 30, 25)]];
        }
        else
        {
            [cell.contentView addSubview:[self img_name:@"chk_box.png" frm:CGRectMake(140, 3, 30, 25)]];
        }

    }
    else if ([selected_table_string isEqualToString:@"table_for_myselection"])
    {/////IT'S RECIEVED FOR MYSELECTIONS......
        //ParcelDetails *obj_parcel_selected_for_mail;
        obj_parcel_details_for_mail = [AppDelegate.mySelected objectAtIndex:indexPath.row];
        //cell.textLabel.text = [AppDelegate.mySelected objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@",obj_parcel_details_for_mail.parcelId];
        cell.textLabel.font = [UIFont fontWithName:@"Tahoma" size:18.0];


        UIButton *mail_to_Button =  [UIButton buttonWithType:UIButtonTypeCustom];
        [mail_to_Button setImage:[UIImage imageNamed:@"mail_icon_9"] forState:UIControlStateNormal];
        mail_to_Button.layer.shadowColor = [UIColor grayColor].CGColor;
        mail_to_Button.layer.shadowOffset = CGSizeMake(0, 3);
        mail_to_Button.layer.shadowOpacity = 1.8;
        mail_to_Button.layer.shadowRadius = 10.0;
        [mail_to_Button addTarget:self action:@selector(send_mail_Action) forControlEvents:UIControlEventTouchUpInside];
        [mail_to_Button setFrame:CGRectMake(530,15,40,40)];

        [cell.contentView addSubview:mail_to_Button];
    }

    return cell;
}

2 个答案:

答案 0 :(得分:0)

每次显示子单元时,都会将子视图添加到单元格中,并且最终会耗尽内存(这可能是您没有收到错误消息或崩溃的原因)。要解决此问题,我们想到的解决方案就是在您的img_name:frm:方法中标记您创建的子视图。

subview.tag = 100;

并在您的单元格自定义代码中;

 for(UIView *view in cell.subviews){
   if(view.tag = 100)[view removeFromSuperview];
 }

编辑*对你的UIButton应该这样做。

答案 1 :(得分:0)

在更新TableView的数据源之前,只需调用[yourTableView reloadData];这可以在滚动和更新数据源阵列时解决崩溃问题。