NSArray 计数不起作用
所以我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,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.
如果控制台消息没有帮助,请将其分解并进行调试。有时它会有所帮助,特别是在很晚的时候。 IE
Break it down and debug, if console message is not being helpful. It helps sometimes, specially when it's late. i.e.
试试这个:
count 不是一个属性,它是您在数组上运行的方法。
Try this:
count is not a property, it is a method you run on the array.