“块”方法,因为numberOfRowsInSection返回0

时间:2012-12-21 11:35:32

标签: php ios json tableview

我的.h文件:

#import <UIKit/UIKit.h>
#import "ServiceConnector.h"

@interface PartnerTableController : UITableViewController <ServiceConnectorDelegate>

@property (nonatomic, retain) IBOutlet NSMutableArray *allPartners;


- (IBAction)download:(id)sender;

@end

我的.m文件的一部分:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self download:self];
}

...

- (IBAction)download:(id)sender { //perform get request
    ServiceConnector *serviceConnector = [[ServiceConnector alloc] init];
    serviceConnector.delegate = self;
    [serviceConnector download];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSLog(@"count allpartners: %d", [allPartners count]);
    return [allPartners count];
}

问题是numberOfRowsInSection返回0,因为下载我的JSON-String需要一段时间,创建一个对象并将其放入NSMutableArray allPartners。我可以以某种方式阻止方法download

更新:allPartners由此方法填充:

[serviceConnector download];

-(void)download{

    //build up the request that is to be sent to the server
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.bala.com"]];

    [request setHTTPMethod:@"GET"];
    [request addValue:@"getValues" forHTTPHeaderField:@"METHOD"]; //selects what task the server will perform

    //initialize an NSURLConnection  with the request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(!connection){
        NSLog(@"Connection Failed");
    }
}

1 个答案:

答案 0 :(得分:0)

当ServiceConnector通过代理通知您它拥有您执行[table reloadData];的数据时,那就是全部。同时你将有一个空表,可能会在那里显示一些等待的HUD或做一些像if [allPartners count] is 0 then return count 1 and fill the cell with some special content这样的逻辑。