应用在同一 'UITableView' 中因两个自定义 'UITableViewCell' 而崩溃
App crashes with two custom 'UITableViewCell' in the same 'UITableView'
我有一个 'UITableView',应该加载一个 Custom 'UITableViewCell'。当用户触摸其中一个单元格时,触摸的单元格将替换为另一个自定义单元格,并带有额外的信息标签。
但是,当用户触摸单元格时,应用程序崩溃了。它引发了以下异常:
'NSInvalidArgumentException', reason: '-[simplefiedCell cellInfo]: unrecognized selector sent to instance 0x7fb5b2f3d950'
我的 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
代码在哪里:
if(indexPath.row == self.selectedCell) {
regularCell *cell = (regularCell *)[tableView dequeueReusableCellWithIdentifier:cellID1];
[cell.cellInfo setText:@"Some temporally text to test this code"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"regularCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}else{
simplefiedCell *cell = (simplefiedCell *)[tableView dequeueReusableCellWithIdentifier:cellID2];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"simplefiedCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
switch (indexPath.row) {
case 0:
[cell.cellTopic setText:@"Text1"];
break;
case 1:
[cell.cellTopic setText:@"Text2"];
break;
case 2:
[cell.cellTopic setText:@"Text3"];
break;
case 3:
[cell.cellTopic setText:@"Text4"];
break;
case 4:
[cell.cellTopic setText:@"Text5"];
break;
case 5:
[cell.cellTopic setText:@"Text6"];
break;
default:
break;
}
return cell;
}
我在里面使用的简单代码 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == self.selectedCell)
self.selectedCell = -1;
else
self.selectedCell = indexPath.row;
[self.tableView reloadData];
有人知道这是怎么回事吗?感谢任何帮助!
乍一看,我怀疑问题不在您上面粘贴的代码中。机会是:
- 您不小心为 cellID1 注册了 simplifiedCell NIB,并得到了一个简化的单元格 class,而您期待的是一个常规单元格。
- cellID1和cellID2的值相同,导致后面的注册中奖
- 您在 regularCell XIB 中指定了错误的自定义 class,实际上是在实例化 simplifiedCell class。
如果这三个中的 none 是这种情况,那么我建议搜索简化单元格 class 的名称——首先在 XIB xml 文件中,然后在代码——无论你在哪里找到它,问问自己它是否真的应该是那个地方的常规 class。想必其中之一应该是。
我有一个 'UITableView',应该加载一个 Custom 'UITableViewCell'。当用户触摸其中一个单元格时,触摸的单元格将替换为另一个自定义单元格,并带有额外的信息标签。
但是,当用户触摸单元格时,应用程序崩溃了。它引发了以下异常:
'NSInvalidArgumentException', reason: '-[simplefiedCell cellInfo]: unrecognized selector sent to instance 0x7fb5b2f3d950'
我的 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
代码在哪里:
if(indexPath.row == self.selectedCell) {
regularCell *cell = (regularCell *)[tableView dequeueReusableCellWithIdentifier:cellID1];
[cell.cellInfo setText:@"Some temporally text to test this code"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"regularCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}else{
simplefiedCell *cell = (simplefiedCell *)[tableView dequeueReusableCellWithIdentifier:cellID2];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"simplefiedCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
switch (indexPath.row) {
case 0:
[cell.cellTopic setText:@"Text1"];
break;
case 1:
[cell.cellTopic setText:@"Text2"];
break;
case 2:
[cell.cellTopic setText:@"Text3"];
break;
case 3:
[cell.cellTopic setText:@"Text4"];
break;
case 4:
[cell.cellTopic setText:@"Text5"];
break;
case 5:
[cell.cellTopic setText:@"Text6"];
break;
default:
break;
}
return cell;
}
我在里面使用的简单代码 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == self.selectedCell)
self.selectedCell = -1;
else
self.selectedCell = indexPath.row;
[self.tableView reloadData];
有人知道这是怎么回事吗?感谢任何帮助!
乍一看,我怀疑问题不在您上面粘贴的代码中。机会是:
- 您不小心为 cellID1 注册了 simplifiedCell NIB,并得到了一个简化的单元格 class,而您期待的是一个常规单元格。
- cellID1和cellID2的值相同,导致后面的注册中奖
- 您在 regularCell XIB 中指定了错误的自定义 class,实际上是在实例化 simplifiedCell class。
如果这三个中的 none 是这种情况,那么我建议搜索简化单元格 class 的名称——首先在 XIB xml 文件中,然后在代码——无论你在哪里找到它,问问自己它是否真的应该是那个地方的常规 class。想必其中之一应该是。