从数组

时间:2015-07-23 14:28:42

标签: ios objective-c arrays

我有一个数组_locations,它是通过json查询从服务器填充的。我现在希望这个数组填充我创建的表,但它没有,它会退出[self.delegate itemsDownLoaded:_locations];

如果我查看日志,我可以看到_locations确实填充了,但它没有解析该数据。

以下是HomeModel2的完整代码:

#import "HomeModel2.h"
#import "Location2.h"        

@interface HomeModel2(){
        NSMutableData *_downloadedData;
}
@end

@implementation HomeModel2

- (void) downLoadItems{
    // Download the json file
    NSURL *jsonFileUrl = [NSURL URLWithString:@"http://server.com/service_2.php"];

    // Create the request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

    // Create the NSURLConnection
    [NSURLConnection connectionWithRequest:urlRequest delegate:self];

}
#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // Initialize the data object
    _downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the newly downloaded data
    [_downloadedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Create an array to store the locations
    NSMutableArray *_locations = [[NSMutableArray alloc] init];

    // Parse the JSON that came in
    NSError *error;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

    // Loop through Json objects, create question objects and add them to our questions array
    for (int i = 0; i < jsonArray.count; i++)
    {
        NSDictionary *jsonElement = jsonArray[i];

        // Create a new location object and set its props to JsonElement properties
        Location2 *newLocation = [[Location2 alloc] init];
        newLocation.name = jsonElement[@"Name"];
        newLocation.address = jsonElement[@"Address"];
        newLocation.latitude = jsonElement[@"Latitude"];
        newLocation.longitude = jsonElement[@"Longitude"];

        // Add this question to the locations array
        [_locations addObject:newLocation];
    }

    // Ready to notify delegate that data is ready and pass back items
    if (self.delegate)
    {
        [self.delegate itemsDownLoaded:_locations];
    }
}

@end

..这是用于发布数据的ViewController:

#import "ViewController.h"
#import "Location2.h"

@interface ViewController (){
    HomeModel2 *_homeModel;
    NSArray *_feedItems;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set this view controller object as the delegate and data source for the table view
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;

    // Create array object and assign it to _feedItems variable
    _feedItems = [[NSArray alloc] init];

    // Create new HomeModel2 object and assign it to _homeModel variable
    _homeModel = [[HomeModel2 alloc] init];

    // Set this view controller object as the delegate for the home model object
    _homeModel.delegate = self;

    // Call the download items method of the home model object
    [_homeModel downLoadItems];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)itemsDownLoaded:(NSArray *)items
{
    // This delegate method will get called when the items are finished downloading

    // Set the downloaded items to the array
    _feedItems = items;

    // Reload the table view
    [self.listTableView reloadData];
}

#pragma mark Table View Delegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of feed items (initially 0)
    return _feedItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Retrieve cell
    NSString *cellIdentifier = @"BasicCell";
    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // Get the location to be shown
    Location2 *item = _feedItems[indexPath.row];

    // Get references to labels of cell
    myCell.textLabel.text = item.address;

    return myCell;
}

@end

4 个答案:

答案 0 :(得分:0)

你可以在这段代码之后确认有“位置”对象:

// Set the downloaded items to the array
_feedItems = items;

或者甚至没有调用委托方法? 我不明白你的措辞......

答案 1 :(得分:0)

而不是代码

if (self.delegate)
{
    [self.delegate itemsDownLoaded:_locations];
}

使用

// Set the downloaded items to the array
_feedItems = [NSArray arrayWithArray:items];

// Reload the table view
[self.listTableView reloadData];

或更改此行代码并检查

而不是

_feedItems = items;

使用

_feedItems = [NSArray arrayWithArray:items];

希望它可以帮助你......!

答案 2 :(得分:0)

我发现了问题。我认为这一切都与delegate.self方法有关。我读到在代码中的几个地方使用它并不是最好的事情,如果经常更新和编译,引用很容易搞砸。所以我刷新了所有旧的编译数据,并从头开始再次运行应用程序,它工作正常!

答案 3 :(得分:0)

我不相信您曾经定义委托属性或委托的协议。

因此,您无法确保视图控制器响应委托协议中定义的方法。

你能看到视图控制器中的委托方法是否被调用?