在UiTableViewCell上滚动时应用程序崩溃

时间:2013-09-25 09:53:01

标签: iphone uitableview

嗨我的应用程序在滚动tableview上崩溃并使用低内存维护崩溃日志名称。 任何人都可以告诉我如何管理这个。我使用的代码在这里:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    NSLog(@" %i",indexPath.section);

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //cell=nil;
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    UIImageView *cellBg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 90)];
    cellBg.image=[UIImage imageNamed:@"Cellbg.png"];
    [cell addSubview:cellBg];

    UILabel *lblRecipeBorder=[[UILabel alloc]initWithFrame:CGRectMake(105, 5, 640, 80)];
    lblRecipeBorder.backgroundColor=[UIColor blackColor];
    lblRecipeBorder.layer.borderWidth=5.0;
    lblRecipeBorder.layer.cornerRadius=8.0;
    lblRecipeBorder.layer.borderColor=[[UIColor darkGrayColor] CGColor];
    [cell addSubview:lblRecipeBorder];

    UIImageView *RecipeeImg=[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100, 80)];
    RecipeeImg.backgroundColor=[UIColor clearColor];
    RecipeeImg.layer.borderWidth=5.0;
    RecipeeImg.layer.cornerRadius=8.0;
    RecipeeImg.layer.borderColor=[[UIColor darkGrayColor] CGColor];
    RecipeeImg.image=[UIImage imageNamed:[[appdelegate.arrFiderTool objectAtIndex:indexPath.row] objectForKey:@"RecipestepPics"]];
    RecipeeImg.layer.masksToBounds = YES;
    [cell addSubview:RecipeeImg];

    UILabel *lblRecipee=[[UILabel alloc]initWithFrame:CGRectMake(140, 10, 620, 70)];
    lblRecipee.backgroundColor=[UIColor clearColor];
    [lblRecipee setNumberOfLines:2];
    lblRecipee.textColor=[UIColor colorWithRed:255.0/255.0 green:52.0/255.0 blue:179.0/255.0 alpha:1.0];
    //lblRecipee.font=[UIFont boldSystemFontOfSize:18.0];
    lblRecipee.font=[UIFont fontWithName:@"Noteworthy-Bold" size:18.0];
    //[lblMeetingId setFont:[UIFont fontWithName:@"Times New Roman" size:14]];
    lblRecipee.text=[[appdelegate.arrFiderTool objectAtIndex:indexPath.row] objectForKey:@"RecipeName"];
    [cell addSubview:lblRecipee];

    [RecipeeImg release];
    [lblRecipee release];
    [cellBg release];
    [lblRecipeBorder release];

   // }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

2 个答案:

答案 0 :(得分:1)

您应该重复使用标签和图片视图来添加单元格。您可以通过继承UITableViewCell或给出views标记来完成此操作。在我看来,子类化是最好的方法,但是现在我将给你一个标记视图的例子:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    NSLog(@" %i",indexPath.section);

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

   UIImageView *recipeImageView = nil;
   UILabel *recipeLabel = nil;

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

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        UIImageView *cellBg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 90)];
        cellBg.image = [UIImage imageNamed:@"Cellbg.png"];
        [cell addSubview:cellBg];

        UILabel *lblRecipeBorder=[[UILabel alloc]initWithFrame:CGRectMake(105, 5, 640, 80)];
        lblRecipeBorder.backgroundColor=[UIColor blackColor];
        lblRecipeBorder.layer.borderWidth=5.0;
        lblRecipeBorder.layer.cornerRadius=8.0;
        lblRecipeBorder.layer.borderColor=[[UIColor darkGrayColor] CGColor];
        [cell addSubview:lblRecipeBorder];

        UIImageView *recipeImageView =[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100,    80)];
        recipeImageView.backgroundColor=[UIColor clearColor];
        recipeImageView.layer.borderWidth=5.0;
        recipeImageView.layer.cornerRadius=8.0;
        recipeImageView.layer.borderColor=[[UIColor darkGrayColor] CGColor];
        recipeImageView.layer.masksToBounds = YES;
        recipeImageView.tag = 456;
        [cell addSubview:recipeImageView];

        UILabel *recipeLabel=[[UILabel alloc]initWithFrame:CGRectMake(140, 10, 620, 70)];
        recipeLabel.backgroundColor=[UIColor clearColor];
        [recipeLabel setNumberOfLines:2];
        recipeLabel.textColor=[UIColor colorWithRed:255.0/255.0 green:52.0/255.0 blue:179.0/255.0 alpha:1.0];
        recipeLabel.font=[UIFont fontWithName:@"Noteworthy-Bold" size:18.0];
        recipeLabel.tag = 457;
        [cell recipeLabel];

        [recipeImageView release];
        [recipeLabel release];
        [cellBg release];
        [lblRecipeBorder release];
    } else {
        recipeImageView = [cell viewWithTag:456];
        recipeLabel = [cell viewWithTag:457];
    }

    NSDictionary *item = [appdelegate.arrFiderTool objectAtIndex:indexPath.row];

    recipeImageView.image = [UIImage imageNamed:[item objectForKey:@"RecipestepPics"]];
    recipeLabel.text = [item objectForKey:@"RecipeName"];


    return cell;
}

答案 1 :(得分:-1)

变化:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

要:

UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];