2012-02-29 25 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 2012-02-29 07:33:01

+0

按照代碼標籤值將根據更新在indexPath.row上。這不是預期的行爲。哪些值不應該改變? – Ravin 2012-02-29 08:32:40

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]; 
} 
相關問題