让UITableView填充来自另一个类的数据 [英] Getting UITableView to populate with data from another class

查看:98
本文介绍了让UITableView填充来自另一个类的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-C的新手,这是我第一次尝试实现MVC。我有一个模型类,其中l有一个 NSArray ,它将填充来自JSON对象的数据。我想用这个数组中的对象填充我的 UITableView (在我的视图控制器类中)。

I am quite new to Objective-C and this is the first time I have attempted to implement MVC. I have a model class where l have an NSArray which will be populated with data from a JSON object. I want to populate my UITableView (in my view controller class), with objects from this array.

请查看我的代码:

Droplets.h

Droplets.h

@interface Droplets : NSObject {
    NSArray *dropletsArray;

}

// Get droplets data
- (void) getDropletsList;

//Object initilization
- (id) init;

//Public properties
@property (strong, nonatomic) NSArray *dropletsArray; // Used to store the selected JSON data objects

@end

Droplets .m

Droplets.m

#define kBgQueue dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kDigialOceanApiURL [NSURL URLWithString:@"http://inspiredwd.com/api-test.php"] //Droplets API call

#import "Droplets.h"

@interface Droplets ()

//Private Properties
@property (strong, nonatomic) NSMutableData *data; // Used to store all JSON data objects
@end


@implementation Droplets;

@synthesize dropletsArray;
@synthesize data;

- (id)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

- (void) getDropletsList {

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = kDigialOceanApiURL; // Predefined Digital Ocean URL API http request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self]; //Should be: [[NSURLConnection alloc]initiWithRequest:request delegate:self]; ...however the instance of NSURLConnection is never used, which results in an "entity unsed" error.
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    data = [[NSMutableData alloc]init]; // mutable data dictionary is allocated and initilized
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {

    [data appendData:theData]; // append 'theData' to the mutable data dictionary
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    //JSON foundation object returns JSON data from a foundation object. Assigned returned data to a dictionary 'json'.
    NSDictionary* jsonData = [NSJSONSerialization JSONObjectWithData:data
                                                             options:kNilOptions error:0];

    self.dropletsArray = [jsonData objectForKey:@"droplets"]; //dictionary of arrays
    NSLog(@"Droplets %@", self.dropletsArray);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // If the application is unable to connect to The Digital Ocean Server, then display an UIAlertView
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Unable to connect to The Digital Ocean Server, please ensure that you are connected via either WIFI or 3G." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];

    [errorView show];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; // Turn of the network activity indicator
}

@end

DropletsList.h

DropletsList.h

    @class Droplets;

    @interface DropletsList : UITableViewController 

    - (Droplets *) modelDroplets;

    @end

DropletsList.m

DropletsList.m

    #define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

    @interface DropletsList ()

    //Private properties
    @property (strong, nonatomic) Droplets *modelDroplets;
    @property (strong, nonatomic) NSArray *tableData;

    @end

@implementation DropletsList

@synthesize tableData;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        NSLog(@"get my data from model");
    }
    return self;
}

- (Droplets *) modelDroplets
{
    if (!_modelDroplets) _modelDroplets = [[Droplets alloc]init];
    return _modelDroplets;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _modelDroplets = [[Droplets alloc]init];
    self.tableData = [_modelDroplets dropletsArray];
    [_modelDroplets getDropletsList];

    [self.tableView reloadData]; // reload the droplets table controller

}


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

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {

    return 1; // Return the number of sections.

}


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {

    return [_modelDroplets.dropletsArray count]; // Return the number of rows in the section.
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // The cell identified by "dropletsList", is assiged as the UITableViewCell
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"dropletsList"];

    //NSLog(@"Droplets Name: %@",self.dropletsArray);

    // The UITableView text label is assigned the contents from 'dropletsArray', with the object key "name"- name of the droplet
    cell.textLabel.text=[[tableData objectAtIndex:indexPath.row]objectForKey:@"name"];

    // The UITableView text detail label is assigned the contents from 'dropletsArray', with the object key "status"- status of the droplet
    cell.detailTextLabel.text=[[tableData objectAtIndex:indexPath.row]objectForKey:@"status"];

    //Evalulate the status of each droplet, setting the colour appropriate to the staus
    if ([[[tableData objectAtIndex:indexPath.row] objectForKey:@"status"] isEqualToString:@"active"]) {

        //Set the detail text label colour
        cell.detailTextLabel.textColor = RGB (35,179,0);
    }
    return cell;

}

@end

基本上我的表格不会填充。请有人帮忙吗?

Basically my table doesn't populate. Please could someone help?

推荐答案

- (void)viewDidLoad
{
    [super viewDidLoad];
    _modelDroplets = [[Droplets alloc]init];
    self.tableData = [_modelDroplets dropletsArray];
    [_modelDroplets getDropletsList];

    [self.tableView reloadData]; // reload the droplets table controller

}

在这种方法中你是从网络服务中获取水滴。它是异步的,当tableView重新加载它可能没有完成获取数据的数据时。您需要有一个回调,它将在完成webservice时重新加载tableView。

In this method you are fetching droplets from a webservice. It is asynchronous, by the time tableView reloads the data it might not have completed fetching the data. You need to have a callback which will reload the tableView on completion of webservice.

编辑:

创建一个 Droplets 中的类方法以获取所有数据

Create a class method in Droplets to fetch all data

//Droplets.h
typedef void (^NSArrayBlock)(NSArray * array);
typedef void (^NSErrorBlock)(NSError * error);

//Droplets.m
+ (void)getDropletsWithCompletion:(NSArrayBlock)arrayBlock onError:(NSErrorBlock)errorBlock
{
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:kDigialOceanApiURL];
    [urlRequest setHTTPMethod:@"GET"];
    [urlRequest setCachePolicy:NSURLCacheStorageNotAllowed];
    [urlRequest setTimeoutInterval:30.0f];
    [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

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

                               if (error) {
                                   errorBlock(error);
                               }else{
                                   NSError *serializationError = nil;
                                   NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
                                                                                        options:NSJSONReadingAllowFragments
                                                                                          error:&serializationError];

                                   arrayBlock(json[@"droplets"]);
                               }

                           }];

}

//DropletsList.h

- (void)viewDidLoad
{
    [super viewDidLoad];

    [Droplets getDropletsWithCompletion:^(NSArray *array) {
          self.modelDroplets = droplets;
          [self.tableView reloadData];
     } onError:^(NSError *error) {

           UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
           [alert show];
    }];

}

免责声明:经过测试和验证:)

Disclaimer : Tested and verified :)

这篇关于让UITableView填充来自另一个类的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆