Почему Custom UITableViewCell вызывает исключение NSInvalidArgumentException? - PullRequest
1 голос
/ 29 февраля 2012

Я создал пользовательский 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;
}

Пожалуйста, помогите мне в этом ... Спасибо заранее

Ответы [ 2 ]

2 голосов
/ 29 февраля 2012

Проблема заключается в следующей строке кода:

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

вы должны использовать

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Потому что, когда вы проверяете на ноль, оно никогда не будет равно нулю (оно не войдет вif case) и создается объект UITableViewCell, а не объект пользовательской ячейки.Но это не то, что вы хотите.Вы хотите загрузить ячейку из пера и удалить ее из очереди для повторного использования.

0 голосов
/ 29 февраля 2012

См. Ваш код, вам нужно поменять FewLines на Given Lines, а остальные останутся прежними. Это потому, что вы делаете некоторые ошибки, просто сравните ваш код с приведенным ниже кодом.

- (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];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...