【发布时间】:2015-01-28 04:53:11
【问题描述】:
奇怪的问题,我的应用程序有一个包含三个部分的 UITableView,每个部分由一个单独的数组填充。最终目标是能够将单元格从一个部分移动到另一个部分,所以我在 UITableViewController 中编写了“逻辑”。不幸的是,如果我尝试将一个单元格移动到一个空白部分,它会因错误[__NSCFString name]: unrecognized selector sent to instance 而崩溃。我无法弄清楚我的代码中的缺陷,所以我想我会把它带到这里,而不是花更多时间在错误的方向上进行调试。
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
if (sourceIndexPath.section == 0) {
Athlete *athlete = players[sourceIndexPath.row];
NSLog(@"sta count: %lu", (unsigned long)[players count]);
if (athlete != nil) {
if (destinationIndexPath.section == 1) {
[players removeObjectAtIndex:sourceIndexPath.row];
[benched insertObject:athlete.name atIndex:destinationIndexPath.row];
}else if (destinationIndexPath.section == 2){
[players removeObjectAtIndex:sourceIndexPath.row];
[reserved insertObject:athlete.name atIndex:destinationIndexPath.row];
}
}
} else if (sourceIndexPath.section == 1) {
Athlete *athlete = benched[sourceIndexPath.row];
NSLog(@"Benched count: %lu", (unsigned long)[benched count]);
if (destinationIndexPath.section == 0) {
[benched removeObjectAtIndex:sourceIndexPath.row];
[players insertObject:athlete.name atIndex:destinationIndexPath.row];
}else if (destinationIndexPath.section == 2){
[benched removeObjectAtIndex:sourceIndexPath.row];
[reserved insertObject:athlete.name atIndex:destinationIndexPath.row];
}
}else if (sourceIndexPath.section == 2) {
Athlete *athlete = reserved[sourceIndexPath.row];
NSLog(@"Res count: %lu", (unsigned long)[reserved count]);
NSLog(@"From starter");
if (destinationIndexPath.section == 1) {
NSLog(@"To bench");
[reserved removeObjectAtIndex:sourceIndexPath.row];
[benched insertObject:athlete.name atIndex:destinationIndexPath.row];
}else if (destinationIndexPath.section == 0){
NSLog(@"To reserves");
[reserved removeObjectAtIndex:sourceIndexPath.row];
[players insertObject:athlete.name atIndex:destinationIndexPath.row];
}
/* [players removeObjectAtIndex:sourceIndexPath.row];
[self.tableView reloadData];
[players insertObject:athlete.name atIndex:destinationIndexPath.row];*/
}
}
注意事项:
* 所有的 NSMutableArrays 都是(nonatomic, retain).
* 确切的故障线是[NSMutableArray insertObject:athlete.name atIndex: destinationIndexPath.row];。
* Athlete *athlete 是一个具有 NSString 属性的对象,例如 athlete.name。
【问题讨论】:
-
所以
athlete是NSString而不是Athlete -
@BryanChen 运动员是一个具有“已分配属性”的 NSObject,例如,运动员名称是 NSString,运动员不是 NSString。
-
这是您所期望的,但显然不会发生,否则您将看不到该错误。
-
@BryanChen,这很奇怪,我在第一个“if”块中设置了一个检查(如果运动员!= nil)。可能发生的情况是运动员对象没有属性,但我看不出这是怎么发生的。
-
只是
NSLog("%@ - %@", [athlete class], athlete)
标签: ios objective-c uitableview nsmutablearray nsindexpath