NSArray 计数不起作用

发布于 2024-12-17 14:14:06 字数 1504 浏览 3 评论 0原文

所以我有一个 UITableView,并使用 .plist 中的数据填充表格视图。直到今天,这对我来说一直工作得很好,当我尝试做同样的事情时,我无法让 numberOfRowsInSection 方法工作。这是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     if...

     else if (segmentOptions == exchange){
         NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:section]]];
         return [array count];
     }   

   //Else
    return contentArray.count;
}

所以在该代码中,每次运行它时,代码都会崩溃。但是,我基本上使用相同的代码来设置表格视图文本并且工作正常:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
     }

     if...

     else if (segmentOptions == exchange) {
           NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:indexPath.section]]];
           cell.textLabel.text =  [array objectAtIndex:indexPath.row];
     }

     return cell;
}

并且该代码工作正常,这就是我如此困惑的原因。当我将行数设置为返回特定数字并运行其余代码时,一切正常。所以我真的迷路了。提前致谢

更新 我使用正确的 count 表示法进行了尝试,return [array count];,但仍然遇到相同的崩溃和相同的控制台输出。

So I have a UITableView, and I populate the tableview with data from a .plist. This has been working fine for me until today, when I tried to do the same and I can't get the numberOfRowsInSection, method to work. Here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     if...

     else if (segmentOptions == exchange){
         NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:section]]];
         return [array count];
     }   

   //Else
    return contentArray.count;
}

So in that code, everytime I run it, the code crashs. But, I use basiacally the same code to set the tableview text and that works fine:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
     }

     if...

     else if (segmentOptions == exchange) {
           NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:indexPath.section]]];
           cell.textLabel.text =  [array objectAtIndex:indexPath.row];
     }

     return cell;
}

And that code works fine, which is why I'm so confused. When I set the number of rows to return to a specific number, and run the rest of the code, everything works fine. So I'm really lost. Thands in advance

UPDATE
I tried it with the proper count notation, return [array count];, and I still get the same crash and the same console output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

兔小萌 2024-12-24 14:14:07

首先,count 不是一个属性,因此您不应该使用点语法来访问它。

我建议更改您的代码,以便您不会像属性一样访问 count 方法。

其次,

测试你的数组是否为零并且它是否是一个数组。

第三,

发布实际的完整堆栈跟踪。

First, count is not a property so you should not be using dot syntax to access it.

I would suggest changing your code so that you are not accessing the count method like a property.

Second,

Test to see if your array is nil and that it is even an array.

Third,

Post the actual complete stack trace.

万人眼中万个我 2024-12-24 14:14:07

如果控制台消息没有帮助,请将其分解并进行调试。有时它会有所帮助,特别是在很晚的时候。 IE

// NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:section]]];

NSLog(@"%d", section);

id objAtIndex = [listOfExchanges objectAtIndex:section];
NSLog(@"%@", listOfExchanges);
NSLog(@"%@", objAtIndex);

id objForKey = [exchangeDictionary objectForKey:objAtIndex];
NSLog(@"%@", exchangeDictionary);
NSLog(@"%@", objForKey);

Break it down and debug, if console message is not being helpful. It helps sometimes, specially when it's late. i.e.

// NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:section]]];

NSLog(@"%d", section);

id objAtIndex = [listOfExchanges objectAtIndex:section];
NSLog(@"%@", listOfExchanges);
NSLog(@"%@", objAtIndex);

id objForKey = [exchangeDictionary objectForKey:objAtIndex];
NSLog(@"%@", exchangeDictionary);
NSLog(@"%@", objForKey);
余罪 2024-12-24 14:14:07

试试这个:

[array count]

count 不是一个属性,它是您在数组上运行的方法。

Try this:

[array count]

count is not a property, it is a method you run on the array.

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