tableView上的EXC_BAD_ACCESS(SIGSEGV):cellForRowAtIndexPath

时间:2012-07-27 16:18:56

标签: ios uitableview crash

我已经开启了NSZombieEnabled,当发生以下情况时,我似乎遇到了崩溃。

转到"设置"通过标签栏查看控制器并从那里进入罪魁祸首视图控制器,然后单击另一个选项卡(之前不返回设置视图控制器),然后单击标签栏上的"设置"试。

这是tableView:cellForRowAtIndexPath

的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"temp"] autorelease];

cell.selectionStyle = UITableViewCellSelectionStyleGray;

if(indexPath.section == 1 && [PMSessionManager instance].paymentMethods.cards != nil)
{
    if([[PMSessionManager instance].paymentMethods.cards count] > indexPath.row)
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        //Reverse the array if it's in the wrong order
        if([[[[PMSessionManager instance].paymentMethods.cards objectAtIndex:indexPath.row] cardStatus] isEqualToString:@"Secondary"] && indexPath.row == 0)
        {
            NSArray* reversedArray = [[[PMSessionManager instance].paymentMethods.cards reverseObjectEnumerator] allObjects];
            [PMSessionManager instance].paymentMethods.cards = reversedArray;
        }

        PMCardInfo *cardInfo = [[PMSessionManager instance].paymentMethods.cards objectAtIndex:indexPath.row];

        if([cardInfo.cardStatus isEqualToString:@"Primary"] && indexPath.row == 1)
            cardInfo = [[PMSessionManager instance].paymentMethods.cards objectAtIndex:(indexPath.row -1)];

        UILabel *cardLabel =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     cell.indentationWidth,
                     0.075 * (tableView.rowHeight - 2 * 14),
                     200,
                     26)]
         autorelease];
        [cell.contentView addSubview:cardLabel];
        cardLabel.text = [NSString stringWithFormat:@"%@", [cardInfo.cardStatus capitalizedString]];
        cardLabel.backgroundColor = [UIColor clearColor];
        cardLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        cardLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];


        NSString *cardTypeString = [NSString stringWithFormat:@"%@", [cardInfo.cardType capitalizedString]];

        CGSize cardTypeStringSize = [cardTypeString sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]];

        UILabel *cardTypeLabel =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     cell.indentationWidth,
                     0.63 * (tableView.rowHeight - 2 * 14),
                     cardTypeStringSize.width,
                     24)]
         autorelease];
        [cell.contentView addSubview:cardTypeLabel];
        cardTypeLabel.text = [NSString stringWithFormat:@"%@", [cardInfo.cardType capitalizedString]];
        cardTypeLabel.backgroundColor = [UIColor clearColor];
        cardTypeLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        cardTypeLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];

        UILabel *digitsLabel =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     cardTypeStringSize.width + 4 + cell.indentationWidth,
                     0.6 * (tableView.rowHeight - 2 * 14),
                     150,
                     26)]
         autorelease];
        [cell.contentView addSubview:digitsLabel];
        digitsLabel.text = [NSString stringWithFormat:@"ending with %@", cardInfo.last4Digits];
        digitsLabel.backgroundColor = [UIColor clearColor];
        digitsLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        digitsLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];

        //Let's make the expiration year just two digits
        if(cardInfo.expYear.length == 4)
            cardInfo.expYear = [cardInfo.expYear substringFromIndex:2];

        UILabel *expirationLabel =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     cell.indentationWidth,
                     1.0 * (tableView.rowHeight - 2 * 14),
                     150,
                     26)]
         autorelease];
        [cell.contentView addSubview:expirationLabel];
        expirationLabel.text = [NSString stringWithFormat:@"Expires on %@/%@", cardInfo.expMonth, cardInfo.expYear];
        expirationLabel.backgroundColor = [UIColor clearColor];
        expirationLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        expirationLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    }
    else 
    {
        UILabel *cardTypeLabel =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     cell.indentationWidth,
                     0.25 * (tableView.rowHeight - 2 * 14),
                     275.0,
                     24)]
         autorelease];
        [cell.contentView addSubview:cardTypeLabel];
        if([[PMSessionManager instance].paymentMethods.cards count] == 2)
            cardTypeLabel.text = @"Swap Primary and Secondary Cards";
        else
            cardTypeLabel.text = @"Add Credit Card";
        cardTypeLabel.textAlignment = UITextAlignmentCenter;
        cardTypeLabel.backgroundColor = [UIColor clearColor];
        cardTypeLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        cardTypeLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    }
}
else if(indexPath.section == 0)
{
    if([PMSessionManager instance].paymentMethods.wallet != nil)
    {
        if(indexPath.row == 0)
        {
            NSString *balanceString = [Util numberToCurrencyString:[PMSessionManager instance].paymentMethods.wallet.balance];
            NSString *availableBalanceString = @"Available Balance";

            CGSize balanceStringSize = [balanceString sizeWithFont:[UIFont boldSystemFontOfSize:32.0]];
            CGSize availableBalanceStringSize = [availableBalanceString sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]];

            CGFloat textWidth = balanceStringSize.width + 5.0 + availableBalanceStringSize.width;
            CGFloat margin = (275.0 - textWidth)/2;

            UILabel *balanceLabel =
            [[[UILabel alloc]
              initWithFrame:
              CGRectMake(
                         cell.indentationWidth + margin,
                         0.45 * (tableView.rowHeight - 2 * 14),
                         275.0,
                         32)]
             autorelease];
            [cell.contentView addSubview:balanceLabel];
            balanceLabel.text = balanceString;
            balanceLabel.textAlignment = UITextAlignmentLeft;
            balanceLabel.backgroundColor = [UIColor clearColor];
            balanceLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
            balanceLabel.font = [UIFont boldSystemFontOfSize:32.0];

            UILabel *walletBalanceLabel =
            [[[UILabel alloc]
              initWithFrame:
              CGRectMake(
                         balanceStringSize.width + 5 + cell.indentationWidth + margin,
                         0.72 * (tableView.rowHeight - 2 * 14),
                         275.0,
                         24)]
             autorelease];
            [cell.contentView addSubview:walletBalanceLabel];
            walletBalanceLabel.text = availableBalanceString;
            walletBalanceLabel.textAlignment = UITextAlignmentLeft;
            walletBalanceLabel.backgroundColor = [UIColor clearColor];
            walletBalanceLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
            walletBalanceLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if(indexPath.row == 1)
        {
            UILabel *walletSettingsLabel =
            [[[UILabel alloc]
              initWithFrame:
              CGRectMake(
                         cell.indentationWidth,
                         0.25 * (tableView.rowHeight - 2 * 14),
                         275.0,
                         24)]
             autorelease];
            [cell.contentView addSubview:walletSettingsLabel];
            walletSettingsLabel.text = @"Edit Wallet Settings";
            walletSettingsLabel.textAlignment = UITextAlignmentCenter;
            walletSettingsLabel.backgroundColor = [UIColor clearColor];
            walletSettingsLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
            walletSettingsLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
        }
    }
    else 
    {
        if(indexPath.row == 0)
        {
            UILabel *learnMoreLabel =
            [[[UILabel alloc]
              initWithFrame:
              CGRectMake(
                         cell.indentationWidth,
                         0.25 * (tableView.rowHeight - 2 * 14),
                         275.0,
                         24)]
             autorelease];
            [cell.contentView addSubview:learnMoreLabel];
            learnMoreLabel.text = @"Learn More";
            learnMoreLabel.textAlignment = UITextAlignmentCenter;
            learnMoreLabel.backgroundColor = [UIColor clearColor];
            learnMoreLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
            learnMoreLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
        }
        if(indexPath.row == 1)
        {
            UILabel *signUpLabel =
            [[[UILabel alloc]
              initWithFrame:
              CGRectMake(
                         cell.indentationWidth,
                         0.25 * (tableView.rowHeight - 2 * 14),
                         275.0,
                         24)]
             autorelease];
            [cell.contentView addSubview:signUpLabel];
            signUpLabel.text = @"Sign Up for Wallet";
            signUpLabel.textAlignment = UITextAlignmentCenter;
            signUpLabel.backgroundColor = [UIColor clearColor];
            signUpLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
            signUpLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
        }
    }
}

return cell;
}

使用NSZombieEnabled,我在日志中收到此消息:

-[PaymentMethodViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x404ae00

上面的代码片段来自此PaymentMethodViewController。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这个问题的第一个答案似乎解决了这个问题。

Pop to root view controller without animation crash for the table view