在表视图中添加动态单元格时出现问题?
请查看我的代码,我正在使用它在表视图中添加动态单元格添加运行时。在选择我的表视图时,我调用了此方法。
- (void) allServersFound
{
// called from delegate when bonjourservices has listings of machines:
NSArray *newPosts = [[NSArray alloc]initWithObjects:@"A", nil]; // NSArray of machine names;
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];
int i = 0;
for (NSArray *count in newPosts)
{
[tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];
}
[[self tblHome] beginUpdates];
[[self tblHome] insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationNone];
[[self tblHome] endUpdates];
[tempArray release];
}
但这在运行时给了我以下异常: * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSIndexPath _fastCStringContents:]:无法识别的选择器发送到实例 0x4e0e130
Please have a look on my code which i am using to add dynamic cells in my table view add run time. At did select of my table view i have called this method.
- (void) allServersFound
{
// called from delegate when bonjourservices has listings of machines:
NSArray *newPosts = [[NSArray alloc]initWithObjects:@"A", nil]; // NSArray of machine names;
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];
int i = 0;
for (NSArray *count in newPosts)
{
[tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];
}
[[self tblHome] beginUpdates];
[[self tblHome] insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationNone];
[[self tblHome] endUpdates];
[tempArray release];
}
But this give me following Exception at run time:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSIndexPath _fastCStringContents:]: unrecognized selector sent to instance 0x4e0e130
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,使用如下字符串初始化 tempArray:
然后添加如下所示的 indexPaths:
因此传递给 insertRowsAtIndexPaths 方法的数组包含字符串和 indexPaths 对象。我认为这就是例外的原因。
First you initialize your tempArray with strings like this:
Then you add indexPaths like this:
so the array you pass to the insertRowsAtIndexPaths method contains both strings and indexPaths objects. I think this is the reason of the exception.
正如其他人指出的,您的 tempArray 包含
NSString
和NSIndexPath
对象的混合。这是在做其他事情之前需要解决的最明显的事情。您可以使用[NSMutableArray array]
类方法(它是自动释放的,因此您需要在方法末尾删除对[tempArray release]
的调用) 。一个不太明显的事情是,在调用
insertRowsAtIndexPaths
之前,您的 UITableView 的 model 必须更新,否则您会在更短的时间内得到另一个异常明显的地方。As others noted, your
tempArray
contains a mixture ofNSString
andNSIndexPath
objects. This is the most obvious thing that you need to address before you do anything else. You can use[NSMutableArray array]
class method for that (it's autoreleased, so you'll need to remove the call to[tempArray release]
at the end of your method).A less obvious thing is that the model of your UITableView must be updated before you call
insertRowsAtIndexPaths
, otherwise you would get another exception in a much less obvious place.您的 tmparray 包含 NSString 值以及 IndexPath ,请先尝试删除 NSString 。
尝试
用该行替换该行
Your
tmparray
containsNSString
values as well as theIndexPath
s, try removing theNSString
s first.Try replacing the line
with the line