在表视图中添加动态单元格时出现问题?

发布于 2025-01-04 12:41:51 字数 827 浏览 1 评论 0原文

请查看我的代码,我正在使用它在表视图中添加动态单元格添加运行时。在选择我的表视图时,我调用了此方法。

 - (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微暖i 2025-01-11 12:41:51

首先,使用如下字符串初始化 tempArray

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];

然后添加如下所示的 indexPaths:

[tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];

因此传递给 insertRowsAtIndexPaths 方法的数组包含字符串和 indexPaths 对象。我认为这就是例外的原因。

First you initialize your tempArray with strings like this:

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];

Then you add indexPaths like this:

[tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];

so the array you pass to the insertRowsAtIndexPaths method contains both strings and indexPaths objects. I think this is the reason of the exception.

盛夏已如深秋| 2025-01-11 12:41:51

正如其他人指出的,您的 tempArray 包含 NSStringNSIndexPath 对象的混合。这是在做其他事情之前需要解决的最明显的事情。您可以使用 [NSMutableArray array] 类方法(它是自动释放的,因此您需要在方法末尾删除对 [tempArray release] 的调用) 。

一个不太明显的事情是,在调用 insertRowsAtIndexPaths 之前,您的 UITableView 的 model 必须更新,否则您会在更短的时间内得到另一个异常明显的地方。

As others noted, your tempArray contains a mixture of NSString and NSIndexPath 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.

山有枢 2025-01-11 12:41:51

您的 tmparray 包含 NSString 值以及 IndexPath ,请先尝试删除 NSString 。
尝试

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];

用该行替换该行

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:nil];

Your tmparray contains NSString values as well as the IndexPaths, try removing the NSStrings first.
Try replacing the line

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];

with the line

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文