1

カスタムを作成しましたUITableViewCellが、セルをデキューすると、NSInvalidArgumentExceptionがスローされることがあります。

  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell lblMonth]: unrecognized selector sent to instance 0x6aa7df0     

今、私のカスタムUITableViewCellには属性lblMonthがあるので、なぜこのエラーがスローされるのか混乱しています。以下は、セルをデキューするために使用するコードです。

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {   

        CustomCell *cell;

        cell = [(CustomCell*)[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        if (cell == nil){

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"New_Phone" owner:nil options:nil];


            for(id currentObject in topLevelObjects)
            {
                if([currentObject isKindOfClass:[CustomCell class]])
                {
                    cell = (CustomCell *)currentObject;
                    break;
                }
            }
        }


           CGFloat emival= emi;
            CGFloat curinterest = balarray[indexPath.row] * interset;
            if(indexPath.row == tenure){
                emival = balarray[indexPath.row-1] + curinterest;
            }
            CGFloat curamnt = emival - curinterest;
        balarray[indexPath.row]=balarray[indexPath.row]-curamnt;


           [[cell lblMonth]setText:[NSString stringWithFormat:@"%d", indexPath.row+1]];
           [[cell lblEMI]setText:[NSString stringWithFormat:@"%.f", emival]];
           [[cell lblInterest]setText:[NSString stringWithFormat:@"%.f", curinterest]];
           [[cell lblPrinicipal]setText:[NSString stringWithFormat:@"%.f", curamnt]];
           [[cell lblBalance]setText:[NSString stringWithFormat:@"%.f", balarray[indexPath.row]]];

        return cell;
}

これについて私を助けてください...事前に感謝します

4

2 に答える 2

2

問題は次のコード行にあります。

cell = [(CustomCell*)[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

あなたが使用する必要があります

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

nilをチェックすると、nilになることはなく(この場合は入りません)、カスタムセルオブジェクトではなくUITableViewCellオブジェクトが作成されるためです。しかし、それはあなたが望むものではありません。nibからセルをロードし、再利用するためにデキューします。

于 2012-02-29T07:17:44.110 に答える
0

あなたが与えられた行でいくつかの行を変更する必要があるあなたのコードを見てください、そして残りは同じままです。これは、いくつかの間違いを犯しているためです。コードを以下のコードと比較してください。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
 //below Line get cells if they Cells Exist.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//below Line Checking precrated Cells Exist
if (cell == nil) {
   cell = [(CustomCell*)[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault        reuseIdentifier:CellIdentifier];
}
于 2012-02-29T07:24:45.740 に答える