【问题标题】:NSXMLParser NSStringNSXMLParser NSString
【发布时间】:2013-11-16 13:15:08
【问题描述】:

在成功 (:D) 与我的服务器通信后,我正在尝试解析它返回给我的 xml 文件。根据我正在打印的内容,我假设解析进行得相当顺利。问题是我是新手,对 NSXMLParser 非常非常非常陌生,并且无法在模拟器上的设备屏幕的小行部分中全部打印出来。我显示行但它们是空的,其中没有显示任何内容。如果习惯 NSXMLParser 或其他任何东西的人可以给我建议或告诉我它是否变得混乱,请:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [stories count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"sizing"]];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

    NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"module"];

    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

    NSLog(@"link: %@", storyLink);
     //open in safari
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
}

【问题讨论】:

  • 当你说“把[ting]一切都放在屏幕上不起作用”时,它在什么方面不起作用?它不显示所有内容吗?它是否显示,但顺序错误?格式错了吗?我们需要更多关于问题所在的详细信息。
  • 对不起,我没有很好地提出我的问题,除了需要的 18 行之外,它不会在屏幕上显示任何内容,但它们是空的(我将重新编辑我的问题)。
  • 一般来说,如果是不同的主题(例如优化而不是同一段代码的更多问题),您应该发布新问题而不是添加到现有问题中。

标签: objective-c xml cocoa nsxmlparser nsxml


【解决方案1】:

如果您确信您的 xml 响应已被很好地解析(并且[stories count] 返回 18 行的事实似乎至少有一些东西在正常工作)...

在你的 -cellFOrRowAtIndexPath 中试试这个

...
// Set up the cell
NSString *strSizing = [[stories objectAtIndex:indexPath.row]
                                 objectForKey:@"sizing"];
[cell setText:strSizing];
//optional: uncomment the following line
//stories;
//enable breakpoint on this line
return cell;
}

当断点激活时,检查你在strSizing 中得到了什么,或者只是检查整个stories 对象以获得更好的主意。


我现在附近没有我的 xcode,所以请耐心等待我和我的简单建议

【讨论】:

  • 这个建议很棒,马上就奏效了 :D !!!断点节目如果搞砸了,我的索引是错误的,非常感谢!!
  • 太棒了 :) 在这种情况下,我只是这样做......(检查编辑的答案)。无论如何,是的,你的 int 变量看起来确实很乱(但我不确定)
  • 感谢您的建议,:3 我是目标 c 的新手。也许我可以提出一个改进问题来进一步推动这一点?
  • 你可以,我猜,只要它不改变原始问题的上下文......在这种情况下,最好将它作为一个新问题发布(而不是编辑这个问题)。参考:stackoverflow.com/help/dont-ask 在好问题安全区。 :)
猜你喜欢
  • 1970-01-01
  • 2010-12-22
  • 2011-05-22
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多