2012-02-29 3 views
1

я создал обычай UITableViewCell, но когда я из очереди в клетку, иногда он бросает NSInvalidArgumentException:Почему пользовательский 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

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

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

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

+0

я столкнулся с другим вопросом too..when когда я прокручивать значения Tableview получают changed..please помочь мне по этому поводу .... – vsandeepraju

+0

в соответствии с кодом значение меток будет обновляться на основе на indexPath.row. Это не ожидаемое поведение. Какие значения не должны меняться? – Ravin

0

См. Свой код, который вам необходимо изменить. FewLines с заданными линиями и остальными остаются. Это потому, что вы делаете некоторые ошибки, просто сравнивайте свой код с кодом ниже.

- (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]; 
} 
Смежные вопросы