-2

当我滚动 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;
}
4

2 回答 2

0

每次显示单元格时,您都在向单元格添加子视图,并且最终内存不足(这可能是您没有收到错误消息或崩溃的原因)。为了解决这个问题,想到的解决方案是在你的img_name:frm:方法中标记你的子视图,就像这样。

subview.tag = 100;

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

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

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

于 2012-08-24T14:27:27.803 回答
0

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

于 2012-08-24T14:29:51.963 回答