Iphone 为什么自定义UITableViewCell会导致NSInvalidArgumentException?
我创建了一个自定义的Iphone 为什么自定义UITableViewCell会导致NSInvalidArgumentException?,iphone,uitableview,unrecognized-selector,Iphone,Uitableview,Unrecognized Selector,我创建了一个自定义的UITableViewCell,但当我将该单元格出列时,有时它会抛出NSInvalidArgumentException: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell lblMonth]: unrecognized selector sent to instance 0x6aa7df0 现在,我的自定
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;
}
请帮我解决这个问题…提前谢谢问题在于以下代码行:
cell = [(CustomCell*)[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
你应该使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
因为当您检查nil时,它将永远不会为nil(如果是,它将不会进入),并且创建的UITableViewCell对象不是自定义单元格对象。但那不是你想要的。您希望从nib加载单元,并将其出列以供重用。请参阅您需要的代码,以使用给定行更改少数行,其余行保持不变。 这是因为您犯了一些错误,请将您的代码与下面的代码进行比较
- (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];
}
我也面临着另一个问题..当我滚动tableview值时会发生更改..请帮我解决这个问题..根据代码,标签值将根据indexPath.row进行更新。这不是预期的行为吗。哪些值不应更改?