i have created a custom UITableViewCell
, but when I dequeue the cell, sometimes it throws an NSInvalidArgumentException:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell lblMonth]: unrecognized selector sent to instance 0x6aa7df0
Now, my custom UITableViewCell
does have an attribute lblMonth, so I am confused why it is throwing this error. Below is the code I use to dequeue the cell:
-(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;
}
Please help me over this...Thanks in Advance
The problem lies in following line of code:
cell = [(CustomCell*)[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
you should use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Because when you check for nil it will never be nil(it will not go in if case) and a UITableViewCell object is created not the custom cell object. But thats not what you want. You want to load cell from nib and dequeue it for reuse.