gpt4 book ai didi

iphone - iOS - 为每个 tableView 行解析多个 XML 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:09:14 26 4
gpt4 key购买 nike

我有一个 UITableViewController。

我想多次调用一个 URL ( http://webservices.company.nl/api?station=ut )(对于每个火车站),其中“ut”总是不同的(它是车站的代码)。我想每次都将结果放在新的 tableview 行中。 (URL 返回 XML)。

要调用 URL,我使用这个:

                // Create connection
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://webservices.company.nl/api?station=%@", station.stationCode]]] delegate:self];
[urlConnection start];

然后在“connectionDidFinishLoading”中,我使用 NSXMLParser 解析 URL 内容:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:receivedDataFromURL];
[parser setDelegate:self];
[parser parse];
}

我已经实现了所有方法,如“didStartElement”、“didEndElement”,它成功读取了文件中的所有元素。

我的问题:

对我的 TableView 中的每一行执行此操作的最佳方法是什么?如何将结果放入每一行?我不知道最好的结构是什么,因为我想做这个异步。

非常感谢。

最佳答案

这里的模式就像懒加载图片一样。

1) 创建一个像 TrainStation 这样的自定义对象,它应该有一个 NSString 站代码,一些告诉调用者它已经从 Web 服务初始化的函数的 BOOL 属性,以及一个提供 block 完成处理程序的 init 方法。

// TrainStation.h
@interface TrainStation : NSObject

@property (strong, nonatomic) NSString *stationCode; // your two character codes
@property (strong, nonatomic) id stationInfo; // stuff you get from the web service
@property (strong, nonatomic) BOOL hasBeenUpdated;
@property (copy, nonatomic) void (^completion)(BOOL);

- (void)updateWithCompletion:(void (^)(BOOL))completion;
@end

2) 完成处理程序启动一个 NSURLConnection,保存完成 block 以供稍后解析完成...

// TrainStation.m
- (void)updateWithCompletion:(void (^)(BOOL))completion {

self.completion = completion;
NSURL *url = // form this url using self.stationCode
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

[NSURLConnection sendAsynchronousRequest:self queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

}];
}

// TrainStation does it's own parsing, then
- (void)parserDidEndDocument:(NSXMLParser *)parser

self.hasBeenUpdated = YES;
self.completion(YES);
// when you hold a block, nil it when you're through with it
self.completion = nil;
}

3) 包含表格的 View Controller 需要知道表格 View 单元格随心所欲地来来去去,这取决于滚动,因此网络结果的唯一安全位置是模型(TrainStations 数组)

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

// normal stuff, dequeue cell, etc.

// the interesting part

TrainStation *trainStation = self.array[indexPath.row];
if ([trainStation hasBeenUpdated]) {
cell.detailTextLabel.text = [trainStation.stationInfo description];
// that's just a shortcut. teach your train station how to produce text about itself
} else { // we don't have station info yet, but we need to return from this method right away
cell.detailTextLabel.text = @"";
[trainStation updateWithCompletion:^(id parse, NSError *) {
// this runs later, after the update is finished. the block retains the indexPath from the original call
if ([[tableView indexPathsForVisibleRows] containsObject:indexPath]) {
// this method will run again, but now trigger the hasBeenUpdated branch of the conditional
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
}
}];
}
return cell;
}

关于iphone - iOS - 为每个 tableView 行解析多个 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18220117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com